Open In App

Moment.js moment().toString() Method

Last Updated : 28 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads