Moment.js Customize Changing Time Source
In this article, we will learn how to customize the moment.js time source as per oneās requirements.
Customizing/Changing the time source of moment.js: To customize or change the time that moment.js gives, use can assign moment.now() to a custom function that returns the number of milliseconds since the Unix epoch, i.e, January 1, 1970.
Syntax:
moment.now = function () { // Custom function that returns // the number of milliseconds // since Unix epoch }
Return Type: The return data type of your custom function should be a number.
Example 1:
Javascript
const moment = require( 'moment' ); moment.now = function () { return + new Date(2018, 11, 24); // 24 November, 2018 } console.log(moment.now()) |
Output:
Example 2:
Javascript
const moment = require( 'moment' ); moment.now = function () { return + new Date(2020, 11, 24); // 24 November, 2020 } console.log(moment.now()) |
Output:
Reference: https://momentjs.com/docs/#/customization/now/
Contact Us