1

date.toGMTString()用来获取DateTime格林威治标准时间标准。所以我得到的是'Fri, 26 Apr 2013 17:08:35 UTC'这个。我只想在没有文本的情况下显示它'UTC'。请告诉我

4

4 回答 4

7

使用slice

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

顺便说一句,您应该注意到它toGMTString已被弃用并且与 相同toUTCString,并且该方法确实返回了一个依赖于实现的人类可读的 UTC 日期字符串。您不能确定它是否以" UTC"(or " GMT") 结尾,因此您宁愿使用

date.toUTCString().replace(/\s*(GMT|UTC)$/, "")
于 2013-04-29T20:01:20.920 回答
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
于 2014-01-29T17:19:20.400 回答
2

toGMTString()弃用...toUTCString()改用...

date.toUTCString().slice(0, -4)
于 2013-04-29T20:03:36.720 回答
1

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

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

请参阅文档以获取其他格式字符串,包括可本地化的字符串。

于 2013-04-29T20:23:06.643 回答