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)
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/
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
28 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...