Open In App

Moment.js moment().toDate() Method

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().toDate() method is used to return the Moment object as a native JavaScript object. This Date object can then be used with the native Date methods or for support in other libraries.

Syntax:

moment().toDate();

Parameters: This method does not accept any parameters:

Return Value: This method returns the Moment object in the native JavaScript Date format.

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

Example 1:

Javascript




const moment = require('moment');
  
let momentOne = moment();
let momentTwo = moment().add(10, 'days');
let momentThree = moment().add(10, 'hours');
  
console.log(
    "Native Date object of momentOne is:",
    momentOne.toDate()
)
console.log(
    "Native Date object of momentTwo is:",
    momentTwo.toDate()
)
console.log(
    "Native Date object of momentThree is:",
    momentThree.toDate()
)


Output:

Native Date object of momentOne is: 2022-07-10T17:24:21.114Z
Native Date object of momentTwo is: 2022-07-20T17:24:21.114Z
Native Date object of momentThree is: 2022-07-11T03:24:21.115Z

Example 2:

Javascript




const moment = require('moment');
  
let momentA = moment('25/12/2022', 'DD/MM/YYYY');
let momentB = moment({
    year: 2017, month: 5, day: 4,
    hour: 1, minute: 15, second: 30,
    millisecond: 100
});
  
console.log(
    "Native Date object of momentA is:",
    momentA.toDate()
)
console.log(
    "Native Date object of momentB is:",
    momentB.toDate()
)


Output:

Native Date object of momentA is: 2022-12-24T18:30:00.000Z
Native Date object of momentB is: 2017-06-03T19:45:30.100Z

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads