Moment.js moment().inspect() Method
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( 'w3wiki' ); 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(/* w3wiki */)
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/