How to check for two timestamp for the same day? - GeeksforGeeks

06 Aug, 2021
Given two timestamp then we have to find out whether these time are of same date or not. Here we can use the JavaScript Date Object.
Examples: 
Input: TimeStamp1 = 20-04-2020 , 16:04:55 and 
       TimeStamp2 = 20-04-2020 , 10:22:42 

Output: These dates are of same date
Input: TimeStamp1 = 20-04-2020 , 16:04:55 and 
       TimeStamp2 = 20-04-2019 , 10:22:42

Output: These timestamps are not on the same date.
Explanation: These timestamp are of same date i.e. 20-04-2020 In JavaScript Object this date can be convert. 
var D1 = new Date(2020, 04, 20, 16, 04, 55)
var D2 = new Date(2020, 04, 20, 10, 22, 42)
Approach 1: First Check for year, month and date of both the dates. If all are equal then we can say that these two dates are equal. You check this article JavaScript get Date Methods. To get year we have getFullYear() method, for month we have getMonth() method and for date we have getDate() method.
Example: 
Javascript
<script type = "text/javascript" >     // Function to check whether timestamp are on same day    const TimeStampAreOnSameDay = (d1, d2) =>        d1.getFullYear() === d2.getFullYear() &&        d1.getMonth() === d2.getMonth() &&        d1.getDate() === d2.getDate();       // To set two dates to two variables    var d1 = new Date(2020, 4, 20, 16, 4, 55);    var d2 = new Date(2020, 4, 20, 10, 22, 42);     var result = TimeStampAreOnSameDay(d1 , d2);       //To display the final result    if (result === true)        document.write("Time Stamp " + d1 + " and "                       + d2 + " is of same day.");   else    document.write("Time Stamp " + d1 + " and "                       + d2 + " is of different day.");</script>
Output:
Time Stamp Wed May 20 2020 16:04:55 GMT+0530 (India Standard Time) and
Wed May 20 2020 10:22:42 GMT+0530 (India Standard Time) is of same day.
Approach 2: You have to set hours, minutes and seconds all equal to 0 by setHours() method and then compare both.
Example: 
Javascript
<script type = "text/javascript" >     // Function to check whether timestamp are on same day    const TimeStampAreOnSameDay = (d1, d2) =>        d1.setHours(0,0,0,0) === d2.setHours(0,0,0,0);         // To set two dates to two variables    var d1 = new Date(2020, 4, 20, 16, 4, 55);    var d2 = new Date(2020, 4, 21, 10, 22, 42);     var result = TimeStampAreOnSameDay(d1 , d2);         //To display the final result    if (result === true)    document.write("Time Stamp " + d1 + " and "                    + d2 + " is of same day.");    else    document.write("Time Stamp " + d1 + " and "                    + d2 + " is of different day.");     </script>
Output:
Time Stamp Wed May 20 2020 00:00:00 GMT+0530 (India Standard Time) and
Thu May 21 2020 00:00:00 GMT+0530 (India Standard Time) is of different day.
simranarora5sos
javascript-date
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
File uploading in React.js
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?