SoFunction
Updated on 2025-04-03

Two lines of js code output date and time in the specified format


// <summary>
// Format display date and time
// </summary>
// <param name="x">Date and time to be displayed, for example new Date()</param>
// <param name="y">The format to be displayed, such as yyyy-MM-dd hh:mm:ss</param>
function date2str(x,y) {
var z ={y:(),M:()+1,d:(),h:(),m:(),s:()};
return (/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((>1?"0":"")+eval('z.'+(-1))).slice(-(>2?:2))});
}
alert(date2str(new Date(),"yy-M-d h:m:s"));
alert(date2str(new Date(),"yyyy-MM-d h:m:s"));