The Moment.js
or moment
JavaScript library allows us to manipulate date and time. We can use the seconds()
method to return the seconds of a date or time. It returns a number between 0-59.
moment(date).seconds()moment(time).seconds()
date
: This is the date of which we want to get the seconds.time
: This is the time of which we want to get the seconds.This method returns an integer between 0 and 59.
// require the moment moduleconst moment = require("moment")// create some dates and timeconst date1 = moment(); // current date and timeconst date2 = moment('2020.01.01', 'YYYY.MM.DD')const date3 = moment([2010, 1, 14, 15, 25, 50, 125])const date4 = moment(new Date) // current date and timeconst time1 = moment(1318781876406)const time2 = moment({hour: 20, minute: 20, second: 34})// get the seconds of the time and datesconsole.log(date1.seconds())console.log(date2.seconds())console.log(date3.seconds())console.log(date4.seconds())console.log(time1.seconds())console.log(time2.seconds())
moment
module.moment
module.seconds()
method of moment.js
to get the seconds of the dates and time we created. Next, we print the results to the console.