从DateTime标记末尾删除“UTC”文本

时间:2013-04-29 19:55:09

标签: javascript datetime utc

我使用date.toGMTString()DateTime作为GMT标准。所以我得到的是'Fri, 26 Apr 2013 17:08:35 UTC'这个。我只想在没有文字'UTC'的情况下显示它。请让我知道

4 个答案:

答案 0 :(得分:7)

使用slice

date.toGMTString().slice(0, -4)

顺便说一句,你应该注意到toGMTString is deprecated and the same as toUTCString,并且该方法确实返回了一个依赖于实现的人类可读的UTC日期字符串。您无法确定它是否以" UTC"(或" GMT")结尾,因此您可能会使用

date.toUTCString().replace(/\s*(GMT|UTC)$/, "")

答案 1 :(得分:2)

toGMTString() deprecated ...请改用toUTCString() ...

date.toUTCString().slice(0, -4)

答案 2 :(得分:2)

var d= new Date();

d=d.toLocaleString(); 

//Converts a Date object to a string, using locale conventions
//hence the UTC or GMT+.. is removed

答案 3 :(得分:1)

要获得最终的格式选项和跨浏览器一致性,请使用Moment.js

var m = moment(date);
var s = m.format('ddd, D MMM YYYY H:mm:ss');

有关其他格式字符串,请参阅the docs,包括可本地化的字符串。