MomentJs格式落后一天 [英] MomentJs Format One Day Behind
问题描述
我看到了一个与我的问题类似的问题(
您必须使用
实时示例:
var date = moment.utc("2019-05-27T00:00:00Z");$('#date').append($('< p>').html(date.format("DD")));;$('#date').append($('< p>').html(date.local().format("DD")));
< script src ="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js></script>< script src ="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>< div id ="date"></div>
I have seen a question that is similar to mine (Moment.js sets dates to 1 day behind) but I can't seem to apply it.
Essentially, my date gets parsed like this:
var date = moment("2019-05-27T00:00:00Z"); // date is the 27th
When I format it to get the day, expecting the 27th, I instead receive the 26th!
date.format("DD")
Does anyone know why this might be happening and how to correct it?
You must use moment.utc(), the Moment documentation says:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time instead of local time.
moment().format(); // 2013-02-04T10:35:24-08:00 moment.utc().format(); // 2013-02-04T18:35:24+00:00
jsFiddle Output:
Live example:
var date = moment.utc("2019-05-27T00:00:00Z");
$('#date').append($('<p>').html(date.format("DD")));
$('#date').append($('<p>').html(date.local().format("DD")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="date"></div>
这篇关于MomentJs格式落后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!