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 check for two timestamps for the same day in JavaScript?
The Date object is essential in JavaScript applications for creating and manipulating dates according to developer requirements. A common requirement is checking whether two timestamps represent the same day, which is useful for features like daily task tracking or date-based user activity validation.
In this tutorial, we will learn three different approaches to check whether two timestamps are for the same day. This is particularly useful when you need to compare a user's last activity date with the current date.
Using getFullYear(), getMonth(), and getDate() Methods
The most straightforward approach is to compare the year, month, and date components separately using the Date object's built-in methods. If all three components match, the timestamps are from the same day.
Syntax
if (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
) {
// dates are the same day
} else {
// dates are different days
}
Example
<html>
<body>
<h3>Compare year, month, and date to check for two timestamps of same day</h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
// Create test dates
var date1 = new Date();
var date2 = new Date(date1.getTime() - 3000); // 3 seconds earlier
var date3 = new Date(2020, 11, 10); // Different day
function compareTwoDates(date1, date2) {
if (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
) {
output.innerHTML += date1 + " and <br>" + date2 + " <br>are of same day.<br><br>";
} else {
output.innerHTML += date1 + " and <br>" + date2 + " <br>are not of same day.<br><br>";
}
}
compareTwoDates(date1, date2);
compareTwoDates(date1, date3);
</script>
</body>
</html>
Using setHours() Method to Reset Time Components
The setHours() method allows us to set hours, minutes, seconds, and milliseconds to zero, effectively getting the timestamp for the start of each day. If both timestamps have the same start-of-day value, they're from the same day.
Syntax
// Reset time components to zero
date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);
// Compare timestamps
if (date1.getTime() === date2.getTime()) {
// dates are the same day
} else {
// dates are different days
}
Example
<html>
<body>
<h3>Setting hours, minutes, seconds, and milliseconds to zero to check for same day</h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
var date1 = new Date();
var date2 = new Date(date1.getTime() - 3786000); // About 1 hour earlier
function compareTwoDates(d1, d2) {
// Create copies to avoid modifying original dates
let date1Copy = new Date(d1);
let date2Copy = new Date(d2);
// Set hours, minutes, seconds, and milliseconds to zero
date1Copy.setHours(0, 0, 0, 0);
date2Copy.setHours(0, 0, 0, 0);
// Compare timestamps
if (date1Copy.getTime() === date2Copy.getTime()) {
output.innerHTML += d1 + " and <br>" + d2 + "<br>are of same day.<br><br>";
} else {
output.innerHTML += d1 + " and <br>" + d2 + "<br>are not of same day.<br><br>";
}
}
compareTwoDates(date1, date2);
</script>
</body>
</html>
Using toDateString() Method
The toDateString() method returns only the date portion of a timestamp as a string, excluding the time. This is the most concise approach for same-day comparison.
Syntax
if (date1.toDateString() === date2.toDateString()) {
// dates are the same day
} else {
// dates are different days
}
Example
<html>
<body>
<h3>Using toDateString() method to check for two timestamps of same day</h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
var date1 = new Date();
var date2 = new Date(2020, 1, 21, 12, 23, 22); // Different day
var date3 = new Date(date1.getTime() - 7200000); // 2 hours earlier, same day
function checkSameDay(d1, d2, label) {
if (d1.toDateString() === d2.toDateString()) {
output.innerHTML += label + ": Same day<br>";
} else {
output.innerHTML += label + ": Different days<br>";
}
}
checkSameDay(date1, date2, "Current date vs 2020 date");
checkSameDay(date1, date3, "Current date vs 2 hours ago");
</script>
</body>
</html>
Comparison of Methods
| Method | Code Length | Performance | Readability |
|---|---|---|---|
| Component comparison | Medium | Fast | Good |
| setHours() method | Medium | Medium | Good |
| toDateString() | Short | Medium | Excellent |
Conclusion
The toDateString() method provides the most readable and concise solution for comparing dates. For performance-critical applications, the component comparison method is recommended. Choose the approach that best fits your specific requirements and coding style.
