假设我们有这个日期时间:
Let's say we have this datetime:
var d = new Date("Sat Jul 21 2018 14:00:00 GMT+0200");
将其导出为字符串 (console.log(d)) 会导致浏览器之间的结果不一致:
Exporting it as a string (console.log(d)) gives inconsistent results among browsers:
2018 年 7 月 21 日星期六 14:00:00 GMT+0200(巴黎,马德里(heure d'été)) 使用 Chrome
2018 年 7 月 21 日星期六 14:00:00 UTC+0200 使用 Internet Explorer 等
Sat Jul 21 14:00:00 UTC+0200 2018 with Internet Explorer, etc.
因此我们无法将日期时间发送到具有格式不一致的服务器.
so we can't send datetime to a server with an unconsistent format.
那么自然的想法是要求 ISO8601 日期时间,并使用 d.toISOString(); 但它给出了 UTC 日期时间:2018-07-21T12:00:00.000Z 而我想要的是本地时区时间:
The natural idea then would be to ask for an ISO8601 datetime, and use d.toISOString(); but it gives the UTC datetime: 2018-07-21T12:00:00.000Z whereas I would like the local-timezone time instead:
2018-07-21T14:00:00+0200
or
2018-07-21T14:00:00
如何获得这个(不依赖像momentjs这样的第三方依赖)?
我试过这个,似乎可行,但没有更自然的方法吗?
var pad = function(i) { return (i < 10) ? '0' + i : i; };
var d = new Date("Sat Jul 21 2018 14:00:00 GMT+0200");
Y = d.getFullYear();
m = d.getMonth() + 1;
D = d.getDate();
H = d.getHours();
M = d.getMinutes();
S = d.getSeconds();
s = Y + '-' + pad(m) + '-' + pad(D) + 'T' + pad(H) + ':' + pad(M) + ':' + pad(S);
console.log(s);
ECMA-262 对使用时区格式化日期字符串的内置支持有限,存在依赖于实现的 toString 和 toLocaleString 方法或 toISOString,它始终是 UTC.如果 toISOString 允许一个参数来指定 UTC 或本地偏移量(默认为 UTC),那就太好了.
There is limited built-in support for formatting date strings with timezones in ECMA-262, there is either implementation dependent toString and toLocaleString methods or toISOString, which is always UTC. It would be good if toISOString allowed a parameter to specify UTC or local offset (where the default is UTC).
编写自己的函数来生成符合 ISO 8601 且具有本地偏移量的时间戳并不困难:
Writing your own function to generate an ISO 8601 compliant timestamp with local offset isn't difficult:
function toISOLocal(d) {
var z = n => ('0' + n).slice(-2);
var zz = n => ('00' + n).slice(-3);
var off = d.getTimezoneOffset();
var sign = off > 0? '-' : '+';
off = Math.abs(off);
return d.getFullYear() + '-'
+ z(d.getMonth()+1) + '-' +
z(d.getDate()) + 'T' +
z(d.getHours()) + ':' +
z(d.getMinutes()) + ':' +
z(d.getSeconds()) + '.' +
zz(d.getMilliseconds()) +
sign + z(off/60|0) + ':' + z(off%60);
}
console.log(toISOLocal(new Date()));
这篇关于Date.toISOString() 但本地时间而不是 UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)
XMLHttpRequest 的 getResponseHeader() 的限制?Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)