Open In App

Moment.js moment().weekYear() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The moment().weekYear() Method is used to get or set the current week-year of the Moment. This method is Locale-aware meaning that the first day of the week can be a Sunday or Monday depending on the Locale of the Moment. This may cause the week-year to differ as the first day of the first week may not fall on the first day of the year depending on the Locale.

Syntax:

moment().weekYear( Number );

Parameters: This method accepts a single parameter as mentioned above and described below:

  • Number: This is week-year value that the Moment will be set to. It is an optional parameter.

Return Value: This method returns the current week-year of the Moment.

Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

The below examples will demonstrate the Moment.js moment().weekYear() Method.

Example 1:

Javascript




const moment = require('moment');
  
let momentOne = 
    moment("01-01-2022", "MM-DD-YYYY").locale("en");
let weekYearA = momentOne.weekYear();
console.log("weekYearA", weekYearA)
  
// Week-Year when the locale is of brazil
let momentTwo = 
    moment("01-01-2022", "MM-DD-YYYY").locale("br");
let weekYearB = momentTwo.weekYear();
console.log("weekYearB", weekYearB)
  
let momentThree = 
    moment("10-18-1985", "MM-DD-YYYY");
let weekYearC = momentThree.weekYear();
console.log("weekYearC", weekYearC)


Output:

weekYearA 2022
weekYearB 2021
weekYearC 1985

Example 2:

Javascript




const moment = require('moment');
  
let moment1 = moment().weekYear(2010);
console.log("Moment1 is now:", moment1)
  
let moment2 = moment().weekYear(100);;
console.log("Moment2 is now:", moment2)
  
let moment3 = moment().weekYear(1800);;
console.log("Moment3 is now:", moment3)


Output:

Moment1 is now: Moment<2010-06-29T02:02:14+05:30>
Moment2 is now: Moment<0100-06-29T02:02:14+05:53>
Moment3 is now: Moment<1800-07-01T02:02:14+05:53>

Reference: https://momentjs.com/docs/#/get-set/week-year/



Last Updated : 28 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads