Moment.JS - Get date from week number

12,617

Solution 1

Yes, it's possible:

var date = moment('2017').add(13, 'weeks');

Note that moment('2017') returns January 1st of 2017.

Solution 2

Using startOf('isoweek') you will get the first day of the week.

moment('2017').add(13, 'weeks').startOf('week').format('DD MM YYYY'); // "02 04 2017"m, gives you Sunday(last day of the week)

moment('2017').add(13, 'weeks').startOf('isoweek').format('DD MM YYYY');
"27 03 2017"// first day of the week ( gives you Monday)

Solution 3

Another way is to use a format string

var dateLocale = moment(week + " " + year, "ww gggg");
var dateISO = moment(week + " " + year, "WW GGGG");

Solution 4

No need to add: moment().year(2017).week(13);

Share:
12,617
Nicolas
Author by

Nicolas

Student Interactive multimedia design (IMD) at Thomas More, Belgium. Always trying to learn and improve. Curious person, open for a challenge.

Updated on June 23, 2022

Comments

  • Nicolas
    Nicolas almost 2 years

    I don't see this in the documentation of moment.js. Maybe I'm skipping over it but I want to convert a week number in a year to a date format.

    for example

    week: number = 13 
    year: number = 2017
    date: date = // get date format for the first day of that week
    

    I'm using moment.js, but I can't find what I want in the documentation. Is this possible to do? I've found some answers for plain javascript, but since I'm already using moment.js, I figured there might be an easy way to do this

  • Nicolas
    Nicolas about 7 years
    how do I correctly convert this to the date format? moment('2017').add(13, 'weeks'); does not provide me with a date type and using moment(this.dateSelection.otherYear).add(this.dateSelection.‌​otherMonth, "months").toDate(); gives me this result Mon Jun 01 1970 01:00:02 GMT+0200 (Romance Daylight Time) so the year is not correct, the month is correct.
  • ichigolas
    ichigolas almost 7 years
    This method fails because the start of a given year can fall in any weekday.
  • user2276146
    user2276146 over 4 years
    I had same issue with 1970 year. It was also bricking my browser (FF). What helped me here, was passing full date for moment like: let date = moment('2019-01-01).add(13, 'weeks');