在IE 11中使用Momentjs日期格式时,日期出错 [英] Date went wrong when using Momentjs date format in IE 11
问题描述
我在当前日期使用Moment.js格式函数
I am using Moment.js format function on a current date as
var startDate = moment(new Date()).format('MM/DD/YY');
结果为 06/28/20
发生的事情是,在我使用了 new Date(startDate之后,它只保留了年份部分: 20 为 06/28/20 。 )
,结果为星期一6月28日00:00:00 GMT + 0530(印度标准时间) 。
What happens is that it retains only the year part: 20 as "06/28/20", after I used new Date(startDate)
, the result is "Mon Jun 28 1920 00:00:00 GMT+0530 (India Standard Time)".
此后,当我在 06/28/20 上应用另一种格式时:
After this when I applied another format on "06/28/20":
startDate = moment(startDate ).format('MM-DD-YYYY');
结果为 06-28-1920
在Google Chrome和Firefox中,它给出第二次尝试的正确日期为: 06-28-2020 。
In Google Chrome and Firefox it gives the correct date for the second attempt as: 06-28-2020.
我的代码是:
$(document).ready(function() {
var startDate = moment(new Date()).format('MM/DD/YY');
alert("startDate ==="+startDate +"==="+new Date(startDate ));
startDate = moment(startDate ).format('MM-DD-YYYY');
alert("startDate ==="+startDate +"==="+new Date(startDate ));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
推荐答案
请找到以下内容片段,可能对您有帮助
Please find the below snippet , which might help you
$(document).ready(function() {
var startDate = moment(new Date()).format('MM/DD/YY');
alert("startDate ==="+startDate +"==="+new Date(startDate ));
startDate = moment(new Date(startDate)).format('MM-DD-YYYY');
var str="06-28-2020";
alert("startDate ==="+startDate +"==="+new Date(str.replaceAll("-","/")));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
这篇关于在IE 11中使用Momentjs日期格式时,日期出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!