Ivan Ivan - 4 months ago 12
jQuery Question

Receiving a not expected date after formating with Moment.js

I'm having problems formatting a date. I expect to receive the date "12/07/2016" instead of "13/07/2016". Why a day is added to my formatted date?

$("#donationDate").val("2016-07-12 00:00:00.0");
$("#donationDate").val(moment().format('DD-MM-YYYY'));
console.log($("#donationDate").val());


Best wishes,

Ivan

Answer

$("#donationDate").val(moment().format('DD-MM-YYYY'));

You haven't pass anything in params to moment().Therefore by default it returns a moment object corresponding the current moment ( today we are the 13th of July ).

Then you are plugging a .format() that formats the moment object to DD-MM-YYYY. That's why it returns 13-07-2016.

You probably wanted to do something like moment(2016-07-12 00:00:00.0).format('DD-MM-YYYY') which returns 12-07-2016

Comments