The moment().normalizeUnits() method is used to normalize aliases of the units of time supported by Moment. Most of the inbuilt methods of Moment accept these aliases, therefore, this method can be used for extending the library for other methods to maintain consistency with the Moment library.
Syntax:
moment().normalizeUnits( String );
Parameters: This method accepts a single parameter as mentioned above and discussed below:
- String: It is the alias that has to be normalized.
Return Value: This method returns the normalized unit of the given alias.
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().normalizeUnits() Method.
Example 1:
const moment = require( 'moment' );
console.log(moment.normalizeUnits( 'y' ))
console.log(moment.normalizeUnits( 'Y' ))
console.log(moment.normalizeUnits( 'years' ))
console.log(moment.normalizeUnits( 'YEar' ))
console.log(moment.normalizeUnits( 'Month' ))
console.log(moment.normalizeUnits( 'M' ))
console.log(moment.normalizeUnits( 'months' ))
console.log(moment.normalizeUnits( 'd' ))
console.log(moment.normalizeUnits( 'days' ))
console.log(moment.normalizeUnits( 'D' ))
console.log(moment.normalizeUnits( 'Date' ))
|
Output:
year year year year month month month day day date date
Example 2:
const moment = require( 'moment' );
console.log(moment.normalizeUnits( 'm' ))
console.log(moment.normalizeUnits( 'S' ))
console.log(moment.normalizeUnits( 's' ))
console.log(moment.normalizeUnits( 'second' ))
console.log(moment.normalizeUnits( 'ms' ))
console.log(moment.normalizeUnits( 'milliSeconds' ))
|
Output:
minute second second second millisecond millisecond
Reference: https://momentjs.com/docs/#/utilities/normalize-units/