8

How do I get the date of the first day of the week and the date of the last day of the week with Moment.js?

I need to get the date from the first day of the week to the date of the last day of the current week that I'm basing on the current day.

I read the documentation https://momentjs.com/docs/ and I found the weekday () function, but I need to get the first day of the week and the last day of the week based on the current date, how do I do this using Moment?

I need this to filter the data of an Observable according to the period: Month, Week ...

var start_date = moment(this.start_date, 'DD/MM/YYYY').format('YYYY-MM-DD')
var final_date = moment(this.final_date, 'DD/MM/YYYY').format('YYYY-MM-DD')

this.transacoesList = this.transacoesList
  .filter((v) => {

    var release_date = moment(v.release_date, 'DD-MM-YYYY').format('YYYY-MM-DD');

    if (moment(release_date).isBetween(start_date, final_date, null, '[]')) {
      return true;
    }
  });
1

3 Answers 3

21

You are on the right track. The weekday function can be used to get the Monday and Friday of the current week.

var now = moment();
var monday = now.clone().weekday(1);
var friday = now.clone().weekday(5);
var isNowWeekday = now.isBetween(monday, friday, null, '[]');

console.log(`now: ${now}`);
console.log(`monday: ${monday}`);
console.log(`friday: ${friday}`);
console.log(`is now between monday and friday: ${isNowWeekday}`);

Direct link to the Moment.js docs on the weekday function here: https://momentjs.com/docs/#/get-set/weekday/

3
  • 1
    thanks, helped a lot, did not know the clone function () Commented Mar 12, 2018 at 23:27
  • isn't monday at index 0, and friday at index 4?
    – Ziarno
    Commented May 21, 2019 at 11:58
  • 1
    @Ziarno, according to the linked documentation, weekday is locale aware. Depending on your locale, weekday(0) may be either Monday or Sunday. A more portable solution to always select Monday and Friday, regardless of locale, might be to use isoWeekday (isoWeekday(1) and isoWeekday(5)).
    – Mike Hill
    Commented May 22, 2019 at 19:21
18

Getting first & last day of current week is as simple as the following:

let currentDate = moment();
let weekStart = currentDate.clone().startOf('week');
let weekEnd = currentDate.clone().endOf('week');

Then you can format it

let formatted = weekEnd.format('YYYY-MM-DD');

btw, I recommend Luxon over moment for many reasons, one of the biggest one is immutability

1

Moment's weekday function returns 0 when a day is the first day of the week for your locale. Therefore if the weekday is 0 for today's date, you're already at the first day of this week. If it's not 0, simply subtract that many days (Moment has functions for adding and subtracting days from a date) from the current date, and then you'll have the first day of the week.

Once you have the first day of the week, add 6 days to that and you'll have the last day of the week.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.