How to extract the number of minutes from current time in JavaScript?


We humans can understand how the date and time are divided and printed. On theother hand, JavaScript interprets the date based on a timestamp generated from Unix time, which is a value made up of milliseconds.

JavaScript Platform-independent date objects represent a single point in time. A millisecond counter has been included in Date objects since January 1, 1970 UTC.

YYYY-MM-DD
is the most used JavaScript date format.
Timed Dates
YYYY-MM-D DTHH:MM:SSZ.

A date data type is not available in JavaScript. Dates can be set, obtained, and changed using various methods provided on the Date object in JavaScript.

In this tutorial, we will learn how to extract the number of minutes from current time in JavaScript.

Methods in Date()

  • getHours() − The current hour is shown using the today variable. A 24-hour clock is used in this.

  • getMinutes() − Shows the minute reading as it is currently.

  • getSeconds() − Shows the second reading as it is currently.

Syntax

To declare a date variable in JavaScript, use the syntax below.

var date = new Date();

In the above syntax, Date() is the constructor and we have used it to create the object of the Date class.

Now let us see the way to extract minutes.

Using the getMinutes() Method from the Current Time

Here, we will learn to extract minutes from the current time.

Syntax

Follow the syntax below to use the getMinutes() feature.

var date = new Date();
var minutes = date.getMinutes();

Here, the current time is taken at run time using the new Date() method, and the getMinutes() is called from this value to obtain the current minutes.

Algorithm

  • STEP 1 − Invoke a new Date() without any specific time as a parameter for getting the current time.

  • STEP 2 − Use getMinutes() in the current time to display the minutes.

Example

In the below example, we have created an empty Dom element to display the output. Then a script has been written. Here we are trying to extract minutes from the current time. The syntax is the same as given above. Then getMinutes() extracts the value and we get the output displayed in the inner HTML.

<html> <body> <h2>Using the <i>current time's getMinutes()</i> script feature.</h2> <p id="idCurrent"></p> <script> var curDate = new Date(); var curDisplay = document.getElementById('idCurrent'); curDisplay.innerHTML = "Minutes Current " + curDate.getMinutes(); </script> </body> </html>

Using the getMinutes() Method from a Specific Time

Here, we will learn to extract minutes from a specific or custom time.

Syntax

Follow the syntax below to use the getMinutes feature.

var date = new Date(Month dd, yyyy hh:mm:ss);
var minutes = date.getMinutes();

Here, the current time is taken by passing an argument to the new Date() method like in the syntax above and the getMinutes() is called from this value to obtain the specific minutes.

Example

In the below example, we have created an empty Dom element to display the output. Then a script has been written. Here we are trying to extract and display minutes from a specific time. The syntax is as given above. We need to give it as a parameter to the new Date() function here since we need to extract the minutes value from a specific time. Then getMinutes() extracts the value and we get the output displayed in the inner HTML.

<html> <body> <h2>Using <i>specific time's getMinutes()</i> script feature</h2> <p id="idDisplay"></p> <script> var varDate = new Date("November 05, 1985 18:25:01"); var varDisplay = document.getElementById('idDisplay'); varDisplay.innerHTML = "Minutes Specific " + varDate.getMinutes(); </script> </body> </html>

In this tutorial, we have learned to extract minutes from current time as well as from a specific time in JavaScript using the getMinutes() feature. This is a built-in approach and very easy to be followed.

Updated on: 23-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements