Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptHow to calculate the number of days between two dates in JavaScript...

How to calculate the number of days between two dates in JavaScript ?

Calculating the number of days between two dates in JavaScript required to use date object for any kind of calculation. For that, first, get the internal millisecond value of the date using the in-built JavaScript getTime() function. As soon as both the dates get converted, proceed further by subtracting the later one from the earlier one which in turn returns the difference in milliseconds. Later, the final result can be calculated by dividing the difference (which is in milliseconds) of both dates by the number of milliseconds in one day. 

Syntax: 

Date.getTime()

Approach 1: 

  • Define two dates using new Date().
  • Calculate the time difference of two dates using date2.getTime() – date1.getTime();
  • Calculate the no. of days between two dates, divide the time difference of both dates by no. of milliseconds in a day (1000*60*60*24)
  • Print the final result using document.write().

Example 1: The following JavaScript program will illustrate the solution 

javascript




<script type="text/javascript">
    // JavaScript program to illustrate 
    // calculation of no. of days between two date 
      
    // To set two dates to two variables
    var date1 = new Date("06/30/2019");
    var date2 = new Date("07/30/2019");
      
    // To calculate the time difference of two dates
    var Difference_In_Time = date2.getTime() - date1.getTime();
      
    // To calculate the no. of days between two dates
    var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
      
    //To display the final no. of days (result)
    console.log("Total number of days between dates  <br>"
               + date1 + "<br> and <br>" 
               + date2 + " is: <br> " 
               + Difference_In_Days);
      
</script>


Output: 

Total number of days between dates 
Sun Jun 30 2019 00:00:00 GMT-0700 (Pacific Daylight Time)
and 
Tue Jul 30 2019 00:00:00 GMT-0700 (Pacific Daylight Time) is: 
30

Approach 2: 

  • Defined the present date by using the new date() which will give the present date and the Christmas date by date.getFullYear() (this will get the year, 0-11 are the months in JavaScript).
  • if condition in order to calculate the total number of days if Christmas has passed already (this will calculate the no. of days between the present date and the Christmas of the next year).
  • Use Math.round(christmas() – present_date.getTime()) Divided one day’s milliseconds to Calculate the result in milliseconds and then convert it into days

Example 2: In this example, we calculated the number of days until Christmas day. 

javascript




<script type="text/javascript">
    // One day Time in ms (milliseconds)
    var one_day = 1000 * 60 * 60 * 24
      
    // To set present_dates to two variables
    var present_date = new Date();
      
    // 0-11 is Month in JavaScript
    var christmas_day = new Date(present_date.getFullYear(), 11, 25)
      
    // To Calculate next year's Christmas if passed already.
    if (present_date.getMonth() == 11 && present_date.getdate() > 25)
    christmas_day.setFullYear(christmas_day.getFullYear() + 1)
      
    // To Calculate the result in milliseconds and then converting into days
    var Result = Math.round(christmas_day.getTime() 
                                                       - present_date.getTime()) / (one_day);
      
    // To remove the decimals from the (Result) resulting days value
    var Final_Result = Result.toFixed(0);
      
    //To display the final_result value
    console.log("Number of days remaining till christmas <br>" 
               + present_date + "<br> and <br>" 
               + christmas_day + " is: <br> " 
               + Final_Result);
      
</script>


Output: 

Number of days remaining till christmas 
Sun Jun 30 2019 11:33:51 GMT-0700 (Pacific Daylight Time)
and 
Wed Dec 25 2019 00:00:00 GMT-0800 (Pacific Standard Time) is: 
178

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

RELATED ARTICLES

Most Popular

Recent Comments