简体   繁体   English

moment.js在firefox中提供无效日期,但在chrome中没有

[英]moment.js gives invalid date in firefox, but not in chrome

I'm having a strange issue with moment.js. 我对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. 我编写了一个函数来将时间从utc转换为德语时间格式,并且所有内容在chrome中都可以正常工作。 But now I tried it with firefox and here I got an invalid date. 但现在我用firefox尝试了它,在这里我的日期无效。

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 );


}

   / ...

I've seen this answer but can't get it to work either way. 我已经看到了这个答案,但无论如何也无法让它发挥作用。 Can someone help me out? 有人可以帮我吗?

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.js只是委托给Date.parse非ISO-8601字符串: https//github.com/moment/moment /问题/ 1407

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. 因此,假设您使用Moment来解析用户输入或未知格式的其他字段,或者解析非ISO-8601的格式,您将必须明确指定格式以获得有保证的跨浏览器行为。 Otherwise you're diving into the vaguaries of cross-browser Date.parse - the only format that works consistently there is ISO-8601. 否则你会潜入跨浏览器Date.parse - 唯一一致的格式是ISO-8601。

moment(date_string, date_format);

Pass the format while parsing you date with moment. 通过时刻解析您的日期时传递格式。 Example

moment("25/12/1995", "DD/MM/YYYY");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM