Moment.js gives wrong date in firefox but not chrome

I have a weird problem with moment.js. I wrote a function to convert time from utc to german time format and everything seemed to work 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 );


}

   / ...

      

I have seen this answer , but cannot get it to work anyway. Can anyone help me?

+3


source to share


2 answers


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 Date.parse

for non-ISO-8601 strings: https://github.com/moment/moment/issues/1407



So, assuming you are using Moment to parse user input or another field in an unknown format, or to parse a format rather than ISO-8601, you would need to specify the format explicitly to get guaranteed cross browser behavior. Otherwise, you dive into cross-browser vaguaries Date.parse

- the only format that works consistently is ISO-8601.

+10


source


moment(date_string, date_format);

      

Pass the format when parsing the date. Example



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

      

0


source







All Articles