[英]Moment.js incorrect formatting
Moment suddenly started to format dates incorrectly. Moment 突然开始错误地格式化日期。 Like, completely changing them.
就像,完全改变它们。 I don't understand why.
我不明白为什么。 I made a short video so you can see what's going on here: https://youtu.be/WPLDiiWsfAo
我制作了一个简短的视频,以便您可以看到这里发生了什么: https://youtu.be/WPLDiiWsfAo
Here's some of the code that's working wrong:这是一些工作错误的代码:
activeProject() {
let proj = { ...this.$store.state.activeProject };
if (proj.Start) {
debugger;
proj.Start = moment(proj.Start).format("MM/DD/YYYY");
proj.End = moment(proj.End).subtract(1, "days").format("MM/DD/YYYY");
return proj;
} else return {};
},
proj.Start = "2021-03-01T00:00:00.000Z"
outputs 02/28/2021
proj.Start = "2021-03-01T00:00:00.000Z"
输出02/28/2021
proj.End = "2021-03-08T00:00:00.000Z"
outputs 03/06/2021
proj.End = "2021-03-08T00:00:00.000Z"
输出03/06/2021
So after formatting with moment, it takes 1 days off of proj.Start
and proj.End
.因此,在使用时刻格式化后,
proj.Start
和proj.End
需要 1 天的时间。 Why would it be doing this?为什么会这样做?
In the ISO 8601 standard, the Z
means UTC (Coordinated Universal Time) , which in practice is London, England Time.在 ISO 8601 标准中,
Z
表示 UTC(协调世界时) ,实际上是英国伦敦时间。
Your issue is reproduced below if you are in a timezone west of London, England (like me) and the current time time in London just passed 00:00 (like right now as I write this answer).如果您位于英国伦敦以西的时区(如我)并且伦敦的当前时间刚刚过去 00:00(就像我写这个答案时的现在),您的问题将在下面复制。 That snippet would ouput the right values for one being in a timezone East of London, at the same moment.
该片段将同时输出位于伦敦东部时区的正确值。
let proj = {Start: "2021-03-01T00:00:00.000Z", End:"2021-03-08T00:00:00.000Z"} proj.Start = moment(proj.Start).format("MM/DD/YYYY"); proj.End = moment(proj.End).subtract(1, "days").format("MM/DD/YYYY"); console.log(proj.Start, "expected: 03/01/2021") console.log(proj.End, "expected: 03/07/2021")
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Notice without the Z
now...注意现在没有
Z
...
let proj = {Start: "2021-03-01T00:00:00.000", End:"2021-03-08T00:00:00.000"} proj.Start = moment(proj.Start).format("MM/DD/YYYY"); proj.End = moment(proj.End).subtract(1, "days").format("MM/DD/YYYY"); console.log(proj.Start, "expected: 03/01/2021") console.log(proj.End, "expected: 03/07/2021")
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
So your issue is specififying a date/time relative to UTC while it seems it is not what you want.因此,您的问题是指定相对于 UTC 的日期/时间,而这似乎不是您想要的。
Your solution would be to remove the final Z
from the data used...您的解决方案是从使用的数据中删除最终的
Z
...
moment(...) is local mode.
moment(...) 是本地模式。 Ambiguous input (without offset) is assumed to be local time.
模糊输入(无偏移)被假定为本地时间。 Unambiguous input (with offset) is adjusted to local time.
明确的输入(带偏移量)调整为本地时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.