The moment.js
library is a time manipulation library that provides us with the valueOf()
method. This method returns the number of seconds since the Unix Epoch, such as January 1st, 1970 at 00:00:00 UTC. It converts the time passed to it into a Unix time.
We have the syntax for the valueOf()
method in the following code widget:
time.valueOf()
time
: This is the date or time for which we want to return the number of seconds in milliseconds since the Unix epoch.
An integer, which is in milliseconds, represents a Unix time that is returned.
// require the moment moduleconst moment = require("moment")// create some dates and timeconst time1 = moment([2010, 1, 14, 10, 25, 50, 125])const time2 = moment(new Date) // current date and timeconst time3 = moment(1318781876406)const time4 = moment({hour: 20, minute: 20, second: 34})const time5 = moment([2022, 5, 17])const time6 = moment([2025, 4, 18])const time7 = moment().add(25, "seconds");// get the difference between one time and anotherconsole.log(time1.valueOf())console.log(time2.valueOf())console.log(time3.valueOf())console.log(time4.valueOf())console.log(time5.valueOf())console.log(time7.valueOf())
moment
package.valueOf()
method to get their number of milliseconds since the Unix epoch.