Categories
JavaScript Answers

How to get the week number based on a specific day with Moment.js and JavaScript?

Spread the love

Sometimes, we want to get get week number based on a specific day with Moment.js and JavaScript.

In this article, we’ll look at how to get the week number based on a specific day with Moment.js and JavaScript.

How to get get the week number based on a specific day with Moment.js and JavaScript?

To get the week number based on a specific day with Moment.js and JavaScript, we use the format method.

For instance, we write

const week = moment().format("w");
const week2 = moment().format("W");
const week3 = moment().format("ww");
const week4 = moment().format("WW");

to call format to get the week number of the current date.

'w' and 'W' return a number like 1.

'ww' and 'WW' returns a number like 01.

The upper case format strings returns the ISO week.

Conclusion

To get the week number based on a specific day with Moment.js and JavaScript, we use the format method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *