![]() |
VOOZH | about |
The moment().dayOfYear() method is used to get or set the day of the year of the Moment object. This can be a value between 1 and 366 denoting the first and last possible day of the year. A value outside of this range will set the day for the previous or next years.
Syntax:
moment().dayOfYear( Number );
Parameters: This method accepts a single parameter as mentioned above and described below:
Return Value: This method returns the day of the 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().dayOfYear() Method.
Example 1:
Output:
Current dayOfYear: Mon Jul 18 2022 01:58:29 GMT+0530
Current dayOfYear is: 199
Moment with dayOfYear of 1 is: Sat Jan 01 2022 01:58:29 GMT+0530
Moment with dayOfYear of 365 is: Sat Dec 31 2022 01:58:29 GMT+0530
Moment with dayOfYear of 181 is: Wed Jun 29 2022 01:58:29 GMT+0530
Example 2:
Output:
momentX dayOfYear: Wed Jan 30 2008 01:58:29 GMT+0530
momentX dayOfYear is: 30
momentX with dayOfYear of 450 is: Wed Mar 25 2009 01:58:29 GMT+0530
momentX with dayOfYear of -800 is: Mon Oct 23 2006 01:58:29 GMT+0530