What is moment().minutes() in Javascript?

Overview

In javaScript, the moment.js package or module provides us the ability to manipulate dates and times easily. With its minutes(), also referred to as the minute method, we can get or set the minutes of a date or time.

Syntax

// get minutes
moment(datetime).minutes()
// set minutes
moment(datetime).minutes(Number)
Syntax for minutes() metnod of moment.js

Parameters

  • datetime: This is the date or time we want to get or set its minutes.
  • Number: This is the number of minutes we want to set our date or time. It can only be from 0 to 59. If it is exceeded then it will bubble up to the hour.

Return value

This method returns an integer value from 0 to 59.

Code example 1

Let's look at the code below:

// require the moment module
const moment = require("moment")
// create some dates and time
const date1 = moment(); // current date and time
const 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 time
const time1 = moment(1318781876406)
const time2 = moment({hour: 20, minute: 20, second: 34})
// get the minutes of the time and dates
console.log(date1.minutes())
console.log(date2.minutes())
console.log(date3.minutes())
console.log(date4.minutes())
console.log(time1.minutes())
console.log(time2.minutes())

Code explanation

In the code above:

  • Line 2: We require the moment package.
  • Lines 5 to 10: We create some dates and times using the moment package.
  • Lines 13 to 18: We use the minutes() method to get the minutes of the dates and times created.

Code example 2

Let's look at another example:

// require the moment module
const moment = require("moment")
// create some dates and time
const date1 = moment(); // current date and time
// get the minutes
console.log(date1.minutes())
// now set the minutes
date1.minutes(59)
// get the minutes again
console.log(date1.minutes())

Code explanation

In the code above:

  • Line 2: We require the moment module.
  • Line 5: We create a new date and time that is the current date and time.
  • Line 8: We use the minutes() method to get the minutes of the date we created and print them to the console.
  • Line 11: We now set the minutes to a new value 59.

Free Resources