0

在我的应用程序中,日期以 ISODate 格式存储:

ISODate("2012-04-21T07: 32: 16.285Z")

有这样的字符串最简单的方法是什么:

21/04/2012, 07:32:16

4

2 回答 2

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 < 9,您应该规范化日期 - http://delete.me.uk/2005/03/iso8601.html

于 2012-04-21T08:00:22.423 回答
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"
于 2012-04-21T08:00:42.683 回答