最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment Js Issue : isoWeekYear & isoWeek method behavior - Stack Overflow

programmeradmin5浏览0评论
  1. I fetched the iso week from a date by using moment's isoWeek function.

    moment(new Date(2015,11,28)).isoWeek() //output 53

  2. 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

  3. 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());

  1. I fetched the iso week from a date by using moment's isoWeek function.

    moment(new Date(2015,11,28)).isoWeek() //output 53

  2. 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

  3. 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());

Share Improve this question edited Jan 18, 2016 at 20:45 Scorpio Co. asked Jan 18, 2016 at 19:48 Scorpio Co.Scorpio Co. 211 silver badge3 bronze badges 8
  • 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
 |  Show 3 more ments

2 Answers 2

Reset to default 4

It 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

发布评论

评论列表(0)

  1. 暂无评论