I fetched the iso week from a date by using moment's isoWeek function.
moment(new Date(2015,11,28)).isoWeek() //output 53
I fetched the iso week year from the same date by using moment's isoWeekYear function.
moment(new Date(2015,11,28)).isoWeekYear() //output 2015
But when I gave the same outputs to the input of moment function it results a different date.
moment().isoWeek(53).isoWeekYear(2015).isoWeekday(0).toDate() //output Dec 28 2014
For other dates it is working correctly. Is there anything that I am missing in my code or it is a bug with Moment ?
here is a demo JSFiddle console.log("Iso Week :",moment(new Date(2015,11,28)).isoWeek());
console.log("Iso Year :",moment(new Date(2015,11,28)).isoWeekYear());
console.log("Date :", moment().isoWeek(53).isoWeekYear(2015).isoWeekday(0).toDate());
I fetched the iso week from a date by using moment's isoWeek function.
moment(new Date(2015,11,28)).isoWeek() //output 53
I fetched the iso week year from the same date by using moment's isoWeekYear function.
moment(new Date(2015,11,28)).isoWeekYear() //output 2015
But when I gave the same outputs to the input of moment function it results a different date.
moment().isoWeek(53).isoWeekYear(2015).isoWeekday(0).toDate() //output Dec 28 2014
For other dates it is working correctly. Is there anything that I am missing in my code or it is a bug with Moment ?
here is a demo JSFiddle console.log("Iso Week :",moment(new Date(2015,11,28)).isoWeek());
console.log("Iso Year :",moment(new Date(2015,11,28)).isoWeekYear());
console.log("Date :", moment().isoWeek(53).isoWeekYear(2015).isoWeekday(0).toDate());
- native date does not know anything about moment – sundar Commented Jan 18, 2016 at 19:56
- why are you passing moment to native date? – sundar Commented Jan 18, 2016 at 19:57
- Hi sunder I have added this just to convert the response that is time stamp to date format. – Scorpio Co. Commented Jan 18, 2016 at 20:03
- Thanks for your quick response – Scorpio Co. Commented Jan 18, 2016 at 20:04
- to do that do this moment().isoWeek(53).isoWeekYear(2015).isoWeekday(1).toDate(); – sundar Commented Jan 18, 2016 at 20:06
2 Answers
Reset to default 4It might be the order you've given to the segments. This works:
moment().isoWeekYear(2015).isoWeekday(1).isoWeek(53).toDate());
Check this out from Moment.js docs.
if you chain multiple actions to construct a date, you should start from a year, then a month, then a day etc. Otherwise you may get unexpected results, like when day=31 and current month has only 30 days (the same applies to native JavaScript Date manipulation), the returned date will be 1st of the following month.
Also isoWeekDays go from 1 to 7. By setting 0 you were getting next week's Monday.
first you have to understand one thing.in your first operation whats happening here is,
moment(new Date(2015,11,28)).isoWeek() //output 53
while creating date using new Date() you have passed 11 as month.so what will happen is while creating date month will get incremented by 1.so the date will be 2015-12-28.so the week number is 53.
so for the 3rd operation you have passed the same result.so moment returned the correct date.
in your case if you want to pass month subtract that by 1 in your 1st operation.
moment(new Date(2015,10,28)).isoWeek();
now you will get the correct answer