Jquery本地化UTC日期时间

时间:2014-09-24 08:10:03

标签: jquery date utc datetime-conversion

我正在尝试本地化我的约会,而我正在获得NAN无效日期。那会是什么错误?

My Code in fiddle

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>

1 个答案:

答案 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()

DEMO