javascript两行代码按指定格式输出日期时间

2023-03-14,,

javascript两行代码按指定格式输出日期时间,具体看代码:

function date2str(x,y) {
var z ={y:x.getFullYear(),M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()};
return y.replace(/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-(v.length>2?v.length:2))});
}
alert(date2str(new Date(),"yy-M-d h:m:s"));
alert(date2str(new Date(),"yyyy-MM-d h:m:s"));

  

javascript两行代码按指定格式输出日期时间的相关教程结束。

《javascript两行代码按指定格式输出日期时间.doc》

下载本文的Word格式文档,以方便收藏与打印。