Moment.js Customize Changing Time Source
Last Updated :
02 Sep, 2022
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);
}
console.log(moment.now())
|
Output:
Example 2:
Javascript
const moment = require( 'moment' );
moment.now = function () {
return + new Date(2020, 11, 24);
}
console.log(moment.now())
|
Output:
Reference: https://momentjs.com/docs/#/customization/now/
Share your thoughts in the comments
Please Login to comment...