JavaScript UTC 日期

标签 javascript

我尝试获取 UTC 日期。为什么

(new Date("2015-04-22")).getUTCDate() = 22

还有

(new Date("Apr 22, 2015")).getUTCDate() = 21

最佳答案

引用 MDN 关于 Date.parse 的工作原理(由 new Date("string") 调用):

Differences in assumed time zone

Given a date string of "March 7, 2014", parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC. Therefore Date objects produced using those strings will represent different moments in time unless the system is set with a local time zone of UTC. This means that two date strings that appear equivalent may result in two different values depending on the format of the string that is being converted (this behavior is changed in ECMAScript ed 6 so that both will be treated as local).

请注意我在最后一行突出显示的警告

关于JavaScript UTC 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798914/

相关文章:

javascript - 如何创建带有“下一步”按钮和自动查找文件夹的 HTML 模板演示栏?

javascript - 使用 javascript 替换替换 p 标签中的所有字符

javascript - 如何在文本区域中的行之间绘制分隔符

javascript - Onsen-UI:如何更新屏幕页面属性并重新加载 View ?

javascript - 替代方法而不是 angularjs 中的 $watch

javascript - 与 Play 一起使用的 AngularJs 无法找到或实例化模块

javascript - 类未添加到包含以 x 开头的 id 的父 div

javascript - AppBar 在 React 路由之间消失

Javascript 区域设置日期转换

javascript - 如何在其他 HTML 元素上显示 <section> 标签