Open In App

Moment.js moment().inspect() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The moment().inspect() method is used for debugging of Moment objects by producing a machine-readable string. This string can be used to generate the same Moment object as the current one. It can be also used in Node interactive shells for displaying the Moment object.

Note: In some cases may not work correctly as this method is mostly used for debugging.

Syntax:

moment().inspect();

Parameters: This method does not accept any parameters.

Return Value: This method returns the Moment as a machine-readable 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().inspect() Method.

Example 1:

Javascript




const moment = require('moment');
  
let momentOne = moment();
let momentTwo = moment.utc();
let momentThree = moment().add(10, 'days');
  
console.log(
    "inspect() string of momentOne is:",
    momentOne.inspect()
)
console.log(
    "inspect() string of momentTwo is:",
    momentTwo.inspect()
)
console.log(
    "inspect() string of momentThree is:",
    momentThree.inspect()
)


Output:

inspect() string of momentOne is: moment(“2022-07-10T23:28:10.887”)
inspect() string of momentTwo is: moment.utc(“2022-07-10T17:58:10.888+00:00”)
inspect() string of momentThree is: moment(“2022-07-20T23:28:10.888”)

Example 2:

Javascript




const moment = require('moment');
  
let momentA = moment('GeeksforGeeks');
let momentB = moment('25/12/2022', 'DD/MM/YYYY');
let momentC = moment('1530', 'HHmm');
  
console.log(
    "inspect() string of momentA is:",
    momentA.inspect()
)
console.log(
    "inspect() string of momentB is:",
    momentB.inspect()
)
console.log(
    "inspect() string of momentC is:",
    momentC.inspect()
)


Output:

inspect() string of momentA is: moment.invalid(/* GeeksforGeeks */)
inspect() string of momentB is: moment(“2022-12-25T00:00:00.000”)
inspect() string of momentC is: moment(“2022-07-10T15:30:00.000”)

Reference: https://momentjs.com/docs/#/displaying/inspect/



Last Updated : 20 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads