The Wayback Machine - https://web.archive.org/web/20250117204413/https://www.geeksforgeeks.org/moment-js-moment-tostring-method/
Open In App

Moment.js moment().toString() Method

Last Updated : 28 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The moment().toString() Method is used to return a human-readable string of the Moment object in English. It is similar to the native Date object’s toString() method. The date return contains the Weekday, Month, Day, Year, Time and Timezone Offset.

Syntax:

moment().toString();

Parameters: This method does not accept any parameters:

Return Value: This method returns the Moment as a string.

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().toString() Method.

Example 1:

Javascript




const moment = require('moment');
  
let momentOne = moment();
console.log(
    "MomentOne toString():", momentOne.toString()
)
  
let momentTwo = moment("01-10-2022", "MM-DD-YYYY");
console.log(
    "MomentTwo toString():", momentTwo.toString()
)
  
let momentThree = moment("05:15:44", "hh:mm:ss");
console.log(
    "MomentThree toString():", momentThree.toString()
)


Output:

MomentOne toString(): Tue Jun 28 2022 02:23:53 GMT+0530
MomentTwo toString(): Mon Jan 10 2022 00:00:00 GMT+0530
MomentThree toString(): Tue Jun 28 2022 05:15:44 GMT+0530

Example 2:

Javascript




const moment = require('moment');
  
let moment1 = moment().year(2010);
console.log(
    "Moment1 toString():", moment1.toString()
)
  
let moment2 = moment1.add(10, 'days');
console.log(
    "Moment2 toString():", moment2.toString()
)
  
let moment3 = moment2.add(20, 'hours');
console.log(
    "Moment3 toString():", moment3.toString()
)


Output:

Moment1 toString(): Mon Jun 28 2010 02:17:48 GMT+0530
Moment2 toString(): Thu Jul 08 2010 02:17:48 GMT+0530
Moment3 toString(): Thu Jul 08 2010 22:17:48 GMT+0530

Reference: https://momentjs.com/docs/#/displaying/as-string/


Become a Full-Stack Developer and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

three90RightbarBannerImg