在我的应用程序中,日期以ISODate格式存储:
ISODate(“2012-04-21T07:32:16.285Z”)
如果有一个像这样的字符串,最简单的是什么:
21/04 / 2012,07:32:16
答案 0 :(得分:2)
我喜欢这个脚本http://jacwright.com/projects/javascript/date_format
var d = new Date('2012-04-21T07: 32: 16.285Z'.split(' ').join('')), date;
date = d.format('d/m/Y h:i:s');
console.log(date);
<强> UPD:强> 对于IE&lt; 9你应该将日期标准化 - http://delete.me.uk/2005/03/iso8601.html
答案 1 :(得分:2)
如果您不需要零填充,则可以执行以下操作:
var d = new Date("2012-04-21T07:32:16.285Z");
var formattedDate = d.getUTCDate() + '/'
+ (d.getUTCMonth() + 1) + '/'
+ d.getUTCFullYear() + ', '
+ d.getUTCHours() + ':'
+ d.getUTCMinutes() + ':'
+ d.getUTCSeconds();
// formattedDate is "21/4/2012, 7:32:16"
否则你可以做类似
的事情// zero-pad a two digit integer
function zp(n) {
return (n > 9 ? '' : '0') + n;
}
var d = new Date("2012-04-21T07:32:16.285Z");
var formattedDate = zp(d.getUTCDate()) + '/'
+ zp(d.getUTCMonth() + 1) + '/'
+ d.getUTCFullYear() + ', '
+ zp(d.getUTCHours()) + ':'
+ zp(d.getUTCMinutes()) + ':'
+ zp(d.getUTCSeconds());
// formattedDate is "21/04/2012, 07:32:16"