What is the moment.seconds() method in JavaScript?
Overview
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.
Syntax
moment(date).seconds()moment(time).seconds()
seconds() method of moment.js
Parameters
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.
Return value
This method returns an integer between 0 and 59.
Example
// 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())
Explanation
- Line 2: We require the
momentmodule. - Lines 5–10: We create different dates and times using the
momentmodule. - Lines 13–18: We use the
seconds()method ofmoment.jsto get the seconds of the dates and time we created. Next, we print the results to the console.