December 7, 1999 - Date Calculations | WebReference

# December 7, 1999 - Date Calculations

 December 7, 1999Date Calculations Tips: December 1999 Yehuda Shiran, Ph.D.Doc JavaScript
As explained in Implementing Relative Times (October 22, 1999) and GMT and Local Dates (December 6, 1999), JavaScript's dates are measured in milliseconds. Therefore, date-related calculations deal with large numbers that are hard to understand. We all know that there are 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day, and 7 days in a week. Let's define the following integer constants:

``````var SECOND = 1000; // the number of milliseconds in a second
var MINUTE = SECOND * 60; // the number of milliseconds in a minute
var HOUR = MINUTE * 60; // the number of milliseconds in an hour
var DAY = HOUR * 24; // the number of milliseconds in a day
var WEEK = DAY * 7; // the number of milliseconds in a week``````

The following function calculates the number of days between now and a given date:

``````<SCRIPT LANGUAGE="JavaScript">
<!--
var SECOND = 1000; // the number of milliseconds in a second
var MINUTE = SECOND * 60; // the number of milliseconds in a minute
var HOUR = MINUTE * 60; // the number of milliseconds in an hour
var DAY = HOUR * 24; // the number of milliseconds in a day
var WEEK = DAY * 7; // the number of milliseconds in a week
function daysBetween(yr, mo, dy) {
var nDate = new Date(); // current date (local)
var nTime = nDate.getTime(); // current time (UTC)
var dTime = Date.UTC(yr, mo - 1, dy); // specified time (UTC)
var bTime = Math.abs(nTime - dTime)  // time difference
return Math.round(bTime / DAY);
}
document.write("Days until 2020: " + daysBetween(2020, 1, 1));
// -->
</SCRIPT>``````

The output of this script is:

Even though we're trying to achieve a simple goal, our script is a bit tricky. We embedded comments in the body of the script, using the term "time" as the number of milliseconds since midnight, January 1, 1970 for a specific date. The first statement creates an instance of the `Date` object reflecting the current date and time. The second statement returns the date's time (in milliseconds). Remember that the `getTime()` method refers to the date's GMT value. Now that we've got a hand on the current "time," we need the time of the specified date. We'll use the `Date.UTC()` method. Let's take another look at the statement:

``var dTime = Date.UTC(yr, mo - 1, dy); // specified time (UTC)``

The first argument (`yr`) specifies the year. The second argument specifies the month. Since we want the `daysBetween()` function to receive the actual month as an integer from 1 to 12 (rather than 0 to 11), we need to decrement the value of the function's argument to prepare it for the `UTC()` method (which requires zero-based values). The third argument specifies the day (1 to 31).

The time-related arguments (hours, minutes, seconds) aren't specified. If we want the script to be perfect, we must use the following statement instead:

``````var dTime = Date.UTC(yr, mo - 1, dy, nDate.getUTCHours(),
nDate.getUTCMinutes(), nDate.getUTCSeconds()); // specified time (UTC)``````

However, the `getUTC` methods are only supported in JavaScript 1.3 (Navigator 4.06, Communicator 4.5, and Internet Explorer 5).

Notice the use of the `Math.abs()` method to retrieve the absolute value of the time difference. We don't care if the specified date is in the past or in the future. The `Math.round()` method rounds the final result, because we want the total difference in whole days.