Home  >  Article  >  Web Front-end  >  js两行代码按指定格式输出日期时间_javascript技巧

js两行代码按指定格式输出日期时间_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:00:321354browse
复制代码 代码如下:

//
// 格式化显示日期时间
//

// 待显示的日期时间,例如new Date()
// 需要显示的格式,例如yyyy-MM-dd hh:mm:ss
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"));
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn