Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to extract the number of minutes from current time in JavaScript?
JavaScript uses timestamps based on Unix time (milliseconds since January 1, 1970 UTC) to handle dates and times. While humans understand time in hours and minutes, JavaScript provides built-in methods to extract specific time components from Date objects.
In this tutorial, we will learn how to extract the number of minutes from the current time in JavaScript using the getMinutes() method.
Date Object Methods
The JavaScript Date object provides several methods to extract time components:
getHours() ? Returns the current hour (0-23) in 24-hour format
getMinutes() ? Returns the current minutes (0-59)
getSeconds() ? Returns the current seconds (0-59)
Syntax
To create a Date object and extract minutes:
var date = new Date(); var minutes = date.getMinutes();
Using getMinutes() with Current Time
The getMinutes() method returns the minutes component (0-59) from a Date object. When called on the current time, it gives you the current minute value.
<html>
<body>
<h2>Extract Minutes from Current Time</h2>
<p id="current"></p>
<script>
var currentDate = new Date();
var currentMinutes = currentDate.getMinutes();
document.getElementById('current').innerHTML =
"Current time: " + currentDate.toLocaleTimeString() +
"<br>Minutes: " + currentMinutes;
</script>
</body>
</html>
Using getMinutes() with Specific Time
You can also extract minutes from a specific date and time by passing a date string to the Date constructor:
<html>
<body>
<h2>Extract Minutes from Specific Time</h2>
<p id="specific"></p>
<script>
var specificDate = new Date("November 05, 1985 18:25:01");
var specificMinutes = specificDate.getMinutes();
document.getElementById('specific').innerHTML =
"Specific time: " + specificDate.toLocaleString() +
"<br>Minutes: " + specificMinutes;
</script>
</body>
</html>
Multiple Time Formats Example
Here's an example showing different ways to create Date objects and extract minutes:
<html>
<body>
<h2>Minutes from Different Date Formats</h2>
<div id="results"></div>
<script>
// Current time
var now = new Date();
// Specific date string
var dateString = new Date("2024-03-15 14:30:45");
// Using constructor parameters
var dateParams = new Date(2024, 2, 15, 14, 30, 45); // Month is 0-indexed
var results = document.getElementById('results');
results.innerHTML =
"Current time minutes: " + now.getMinutes() + "<br>" +
"Date string minutes: " + dateString.getMinutes() + "<br>" +
"Constructor params minutes: " + dateParams.getMinutes();
</script>
</body>
</html>
Key Points
The
getMinutes()method returns a value between 0 and 59It works with both current time (no parameters) and specific dates
The method is part of the standard JavaScript Date object
No additional formatting is needed - it returns the raw minute value
Conclusion
The getMinutes() method provides a simple way to extract minutes from JavaScript Date objects. Whether working with current time or specific dates, this built-in method makes time extraction straightforward and reliable.
