我正在尝试本地化我的约会,而我正在获得NAN无效日期。那会是什么错误?
function localizeUTCDateTime(date) {
var utcdate = new Date(date + " UTC");
return utcdate.toDateString() + " " + utcdate.getHours() + ":" + utcdate.getMinutes() + ":" + utcdate.getSeconds();
}
var creationDate = '2014-09-23T00:59:26.92';
$('#divTime').text(localizeUTCDateTime(creationDate));
<div id='divTime'>
</div>
答案 0 :(得分:1)
function localizeUTCDateTime(date) {
var utcdate =new Date(Date.parse(date));
console.log(utcdate);
return utcdate.toUTCString() + " " + utcdate.getHours() + ":" + utcdate.getMinutes() + ":" + utcdate.getSeconds();
}
var creationDate = '2014-09-23T00:59:26.92';
$('#divTime').text(localizeUTCDateTime(creationDate));
首先解析它Date.parse(date)
然后解析新的Date
。要将其转换为字符串,请使用utcdate.toUTCString()
。