var format = function(date, format){
var t = new Date(date);
var tf = function(i){return (i < 10 ? '0' : '') + i};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
switch(a){
case 'yyyy':
return tf(t.getFullYear());
break;
case 'MM':
return tf(t.getMonth() + 1);
break;
case 'mm':
return tf(t.getMinutes());
break;
case 'dd':
return tf(t.getDate());
break;
case 'HH':
return tf(t.getHours());
break;
case 'ss':
return tf(t.getSeconds());
break;
}
})
}
var currdate = '1369065600000';
alert(format(new Date().getTime(currdate), 'yyyy-MM-dd));
远行结果:2015-05-21
本文介绍了一个JavaScript函数,用于将时间戳转换为特定格式的日期字符串。通过定义一个名为varformat的函数,可以将输入的时间戳格式化为如'yyyy-MM-dd'等常见日期格式。
1251

被折叠的 条评论
为什么被折叠?



