33

As mentioned in the title, I have a week number and a year value. Is there a way of finding Monday of that week using moment.js? Was trying, but not succeeded

Bartosz
  • 4,542
  • 11
  • 43
  • 69

4 Answers4

107

Sorry found solution myself:

var test = moment().day("Monday").week(week number here);
Bartosz
  • 4,542
  • 11
  • 43
  • 69
20

Building off the answers provided here...

exports.getDateFromWeek = function(week, year) {
    return moment().day("Monday").year(year).week(week).toDate();
};

Personally, I wanted the first Sunday... and in which case it's:

exports.getDateFromWeek = function(week, year) {
    return moment().day("Sunday").year(year).week(week).toDate();
};
blak3r
  • 16,066
  • 16
  • 78
  • 98
2

Try like this:-

var weekdate= function(year, week, dayNumber)
{
    var j1 = new Date( year,0,10,12,0,0),
        j2 = new Date( year,0,4,12,0,0),
        mon1 = j2.getTime() - j1.getDay() * 86400000;
    return new Date(mon1 + ((week- 1)  * 7  + dayNumber) * 86400000);
};
console.log(weekdate(2010, 1, 4));

2010 starts with Thursady

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 7
    The OP asked for a solution using Moment.js. As far as I can tell, this does not answer the question. – cdeszaq Mar 04 '14 at 15:50
1
moment('2021').add(16, 'weeks').startOf('week').format('DD MM YYYY');

Starts on Sunday. The above code result is "18 04 2021"

moment('2021').add(16, 'weeks').startOf('isoweek').format('DD MM YYYY');

Starts on Monday. The above code result is "19 04 2021"

Sam
  • 149
  • 4
  • 15