Question

I'm trying to replace the functions that I had previously built with date.js, using moment.js. I'm wrapping the dates in moment wrappers like this:

var start = moment(new Date(lastLoadDate.getTime()));
var end = moment(new Date(lastLoadDate.getTime()));

Then, I'm trying to set Datepicker values based upon lastLoadDate. So, for last month, I'm doing:

start = start.day(1);
end = end.day(1).add('months', 1).subtract('days', 1);  
// format dates and set DatePicker values
start = start.format('MM/DD/YYYY');
$('.date_from', context).val(start);
end = end.format('MM/DD/YYYY');
$('.date_to', context).val(end);

This gives me my first error:

end.day(1).add is not a function

However, if I take out part of the end date manipulation

end = end.day(1);

I now get the error:

start.format is not a function

I'm using moment.min.js version 1.1.0.

Était-ce utile?

La solution

Turns out that the documentation is wrong, and that function date() should be used instead of day() to set the date. Instead of writing

end = end.day(1).add('months', 1).subtract('days', 1);

either

end = end.date(1).add('months', 1).subtract('days', 1);

or

end.date(1).add('months', 1).subtract('days', 1);

will work, interchangeably.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top