How to get the first Monday of a month with MomentJS?
Asked Answered
A

4

7

I'm using MomentJS and fullcalendar. I want to get the first Monday of a month. I tried the following code but it doesn't work.

let date = new Date(year, month, 1)
moment(date).isoWeekday(1)
Amparoampelopsis answered 30/8, 2017 at 7:41 Comment(4)
What about the documentation for isoWeekday makes you think it will give you the first Monday of the month? You need to do a bit more work than just setting the current Monday. Give it a try. If you get stuck, ask a specific question.Polynesian
@T.J.Crowder You see? In my code, I try to create a first date of the month and it's should be in first week and isoWeekday will return Monday of the week right?Amparoampelopsis
What if the 1st is a Wednesday? The Monday of that week is in the previous month, and not the first Monday of it. Again: You'll have to do more work. The Moment API gives you plenty to work with.Polynesian
@T.J.Crowder sure I know that the first Monday may in the previous month, that's why I said my code doesn't work, I just asked for a solution and show people what I've already tried. If I did it correctly, why come here? The Moment API gives you plenty to work with << Absolutely, also because of that I need to spend more time on the document to figure out how to solve my problem. Btw, your comments don't make any helps.Amparoampelopsis
A
2

The following code have solved my problem:

let date = moment().set('year', y).set('month', m).set('date', 1).isoWeekday(8)
if(date.date() > 7) { // 
    date = date.isoWeekday(-6)
}
Amparoampelopsis answered 30/8, 2017 at 8:36 Comment(1)
can you explain the logic? what if I want the second Sunday of the month?Unsought
G
5

I believe @xenteros's answer doesn't work for months that begin on a Sunday, because Monday would be the 9th.

Here is a simple fix:

let date = moment().year(y).month(m).date(1).day(8);
if (date.date() > 7)
    date.day(-6);
Guiana answered 20/8, 2020 at 8:46 Comment(0)
A
2

The following code have solved my problem:

let date = moment().set('year', y).set('month', m).set('date', 1).isoWeekday(8)
if(date.date() > 7) { // 
    date = date.isoWeekday(-6)
}
Amparoampelopsis answered 30/8, 2017 at 8:36 Comment(1)
can you explain the logic? what if I want the second Sunday of the month?Unsought
H
1

Here are the steps to get the first monday

  1. Create a day (any day in that specific month)

  2. Get the start of the month, this will return a date

  3. There is two cases for first day, it could be Monday or not Monday

  4. We add 6 days to the first day of the month, if it is Monday, we will get Sunday (same week), else we get a date in the next week that has a Monday (that occure in the same month)

  5. calling startOf('isoWeek') will return the first Monday of that month

     let date = new Date(year, month, 1);
     const firstMondayOfTheMonth = date
        .startOf('month')
        .add(6, 'day')
        .startOf('isoWeek');
    
Helpful answered 2/9, 2022 at 14:49 Comment(0)
I
0

Use moment js. You can pass as parameter any year and month.

import moment from 'moment';

const startOfMonth = moment().year(2021).month(0).startOf('month').isoWeekday(8);
console.log(startOfMonth.format('dddd DD-MM-YYYY')); // Monday 04-01-2021

Month: 0-11.

Irv answered 19/9, 2022 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.