Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to check for two timestamp for the same day?

  • Last Updated : 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: 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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.



My Personal Notes arrow_drop_up
Recommended Articles
Page :