With the moment.js
package, we can manipulate the display of dates and times. The hour()
method is used to get or set the hour of a specific time.
// get hourmoment(time).hour()// set hourmoment(time).hour(number)
time
: This is the time whose hour we want to get or set.
number
: This is the number we want to add to the hour if we want to set it. It can only be from 0
to 23
.
// 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})console.log("before setting hour")// get the hour of the timeconsole.log(time1.hour()) // 10console.log(time2.hour()) // 15console.log(time3.hour()) // 16console.log(time4.hour()) // 20// set hourtime1.hour(23)time2.hour(2)time3.hour(12)time4.hour(4)console.log("After setting hour")// get the hour of the time againconsole.log(time1.hour()) // 10console.log(time2.hour()) // 15console.log(time3.hour()) // 16console.log(time4.hour()) // 20
hour()
method to get the hour of the times we created. Then, we print it to the console.hour()
method again to modify the times we created by resetting their hours.