moment.js在Firefox中给出了无效的日期,但在chrome中没有给出 [英] moment.js gives invalid date in firefox, but not in chrome
问题描述
我在moment.js中遇到了一个奇怪的问题.我编写了一个函数,将时间从utc转换为德国时间格式,并且在chrome中一切似乎都可以正常工作.但是现在我用firefox尝试了一下,在这里得到了无效的日期.
I'm having a strange issue with moment.js. I wrote a function to convert the time from utc to german time format, and everything seemed to work just fine in chrome. But now I tried it with firefox and here I got an invalid date.
moment.locale("de");
$('#from').datepicker({
format: "DD. MMMM YYYY"
});
$('#from').on('change',function() {
var a = moment($('#from').val(), "DD. MMMM YYYY").format("LLLL");
var b = moment(a).add(7,'days');
var localTime = moment.utc(b).toDate();
localTime = moment(localTime).format('DD. MMMM YYYY');
$('#to').val(localTime);
});
$('#to').datepicker({
format:'DD.MMMM YYYY'
});
$('#sendbtn').on('click',function(){
/...
var from = moment(fromfield.value).format();
var to = moment(tofield.value).format();
/...
$('#calendar').fullCalendar( 'gotoDate', from );
getEventDate(from,to,persons.value);
}
});
function getEventDate(start,end,people) {
var Calendar = $('#calendar');
Calendar.fullCalendar();
var Event = {
title:"Your stay for "+people+" people",
allDay: true,
start: start,
end: end
};
filljson(start,end,people);
Calendar.fullCalendar( 'renderEvent', Event );
}
/ ...
我已经看到了这个 answer ,但是不能使它以任何一种方式工作.有人可以帮我吗?
I've seen this answer but can't get it to work either way. Can someone help me out?
推荐答案
从您的问题尚不清楚,哪部分代码引发了错误,但可能的罪魁祸首是Moment.js只是委托给 Date.解析
以获取非ISO-8601字符串: https://github.com/moment/moment/issues/1407
It's not clear from your question which part of the code is throwing the error, but the likely culprit is that Moment.js simply delegates to Date.parse
for non-ISO-8601 strings: https://github.com/moment/moment/issues/1407
因此,假设您正在使用Moment解析用户输入或未知格式的其他字段,或者解析非ISO-8601的格式,则必须明确指定格式以确保交叉浏览器行为.否则,您将陷入跨浏览器的各种问题 Date.parse
-唯一能够始终如一地工作的格式是ISO-8601.
So assuming that you're using Moment to parse user input or another field in an unknown format, or to parse a format that's not ISO-8601, you're going to have to specify the format explicitly to get guaranteed cross-browser behavior. Otherwise you're diving into the vaguaries of cross-browser Date.parse
- the only format that works consistently there is ISO-8601.
这篇关于moment.js在Firefox中给出了无效的日期,但在chrome中没有给出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!