Calculating Dates and Times (JavaScript)

You can use the Date object to perform common calendar and clock tasks, such as comparing dates and calculating elapsed time.

Setting a Date to the Current Date

When you create an instance of the Date object without specifying a date, it returns a value that represents the current date and time, including year, month, day, hour, minute, second, and millisecond. You can then read or modify this date and time.

The following example shows how to instantiate a date without using any parameters and display it in the format mm-dd-yy.

var dt = new Date();  

// Display the month, day, and year. getMonth() returns a 0-based number.  
var month = dt.getMonth()+1;  
var day = dt.getDate();  
var year = dt.getFullYear();  
document.write(month + '-' + day + '-' + year);  

// Output: current month, day, year  

Setting a Specific Date

You can set a specific date by passing a date string to the constructor.

var dt = new Date('8/24/2009');  

// Output: Mon Aug 24 00:00:00 PDT 2009  

The time zone displayed in the date string corresponds to the time zone set on the local machine.

JavaScript is flexible about the format of the string you use as the parameter. For example, you can input "8-24-2009", "August 24, 2009", or "24 Aug 2009".

You can also specify a time. The following example shows one way to specify a date and time in ISO format. The "Z" indicates UTC time.

var dt = new Date('2010-06-09T15:20:00Z');  
document.write("<br />");  

// Output:  
// Wed Jun 09 2010 08:20:00 GMT-0700 (Pacific Daylight Time)  
// 2010-06-09T15:20:00.000Z  

For more information on date formats such as ISO, see Date and Time Strings.

The following example shows other ways to specify a time.

var dtA = new Date('8/24/2009 14:52:10');  

// The parameters are year, month, day, hours, minutes, seconds.  
var dtB = new Date(2009, 7, 24, 14, 52, 10);  

// Output:  
// Mon Aug 24 14:52:10 PDT 2009  
// Mon Aug 24 14:52:10 PDT 2009  

Adding and Subtracting Days, Months, and Years

You can use the getX and setX methods of the Date object to set specific dates and times.

The following example shows how you can set a date to the previous day. Note that if necessary the month and year values are also changed.

var myDate = new Date("1/1/1990");  
var dayOfMonth = myDate.getDate();  
myDate.setDate(dayOfMonth - 1);  


// Output: Sun Dec 31 00:00:00 PST 1989  

The following example sets the date to the last day of the month by subtracting a day from the first day of the next month.


The months of the year are numbered from 0 (January) to 11 (December). The days of the week are numbered from 0 (Sunday) to 6 (Saturday).

var myDate = new Date("1/1/1990")  
myDate.setMonth(myDate.getMonth() + 1);  

myDate.setDate (myDate.getDate() - 1);  


// Output: Wed Jan 31 00:00:00 PST 1990  

Working with Days of the Week

The getDay method gets the day of the week as a number between 0 (Sunday) and 6 (Saturday). (This is not the same as the getDate method, which gets the day of the month as a number between 1 and 31).

The following example sets the date for Thanksgiving, which in the United States is the fourth Thursday in November. The script finds November 1 of the current year, then finds the first Thursday, and then adds three weeks.

var myDate = new Date();  
myDate.setHours(0, 0, 0, 0);  


// Determine November 1.  

// Find Thursday.  
var thursday = 4;  
while(myDate.getDay() != thursday) {  
    myDate.setDate(myDate.getDate() + 1);  

// Add 3 weeks.  
myDate.setDate(myDate.getDate() + 21);  


// Output: Thu Nov 28 00:00:00 <time zone> 2013  

Calculating Elapsed Time

The getTime method returns the number of milliseconds that have elapsed since midnight on January 1, 1970. For any date before that date it returns a negative number.

You can use the getTime method to set a start and end time for calculating an elapsed time. It can be used for measuring small units, such as a few seconds, and large units, such as days.

The following example calculates elapsed time in seconds. The getTime method gets the number of milliseconds since the zero date.

var startTime = new Date('1/1/1990');  
var startMsec = startTime.getMilliseconds();  
var elapsed = (startTime.getTime() - startMsec) / 1000;   

// Output: 5000  

To work with more manageable units, you can divide the milliseconds provided by the getTime method by an appropriate number. For instance, to turn milliseconds into days, divide the number by 86,400,000 (1000 milliseconds x 60 seconds x 60 minutes x 24 hours).

The following example shows how much time has elapsed since the first day of the specified year. It uses division operations to calculate elapsed time in days, hours, minutes, and seconds. It does not account for daylight savings time.

// Set the unit values in milliseconds.  
var msecPerMinute = 1000 * 60;  
var msecPerHour = msecPerMinute * 60;  
var msecPerDay = msecPerHour * 24;  

// Set a date and get the milliseconds  
var date = new Date('6/15/1990');  
var dateMsec = date.getTime();  

// Set the date to January 1, at midnight, of the specified year.  
date.setHours(0, 0, 0, 0);  

// Get the difference in milliseconds.  
var interval = dateMsec - date.getTime();  

// Calculate how many days the interval contains. Subtract that  
// many days from the interval to determine the remainder.  
var days = Math.floor(interval / msecPerDay );  
interval = interval - (days * msecPerDay );  

// Calculate the hours, minutes, and seconds.  
var hours = Math.floor(interval / msecPerHour );  
interval = interval - (hours * msecPerHour );  

var minutes = Math.floor(interval / msecPerMinute );  
interval = interval - (minutes * msecPerMinute );  

var seconds = Math.floor(interval / 1000 );  

// Display the result.  
document.write(days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds.");  

//Output: 164 days, 23 hours, 0 minutes, 0 seconds.  

Determining the User's Age

The following example takes the user's birthday and calculates the user's age in years. It subtracts the birth year from the current year, and then subtracts 1 if the birthday has not occurred yet in the current year.

var birthday = new Date("8/1/1985");  
var today = new Date();  
var years = today.getFullYear() - birthday.getFullYear();  

// Reset birthday to the current year.  

// If the user's birthday has not occurred yet this year, subtract 1.  
if (today < birthday)  
document.write("You are " + years + " years old.");  

// Output: You are <number of years> years old.  

Comparing Dates

When you compare dates in JavaScript, you should keep in mind that the == operator returns true only if the dates on both sides of the operator refer to the same object. Therefore, if you have two separate Date objects set to the same date, date1 == date2 returns false. In addition, a Date object set with only the date and not the time is initialized to midnight of that date. So if you compare one Date set without a specified time to, for example, you should be aware that the first Date is set to midnight and is not.

The following example checks whether the current date is the same, before, or after a specified date. To set the current date in todayAtMidn, the script creates a Date object for the current year, month, and day.

// Get the current date at midnight.  
var now = new Date();   
var todayAtMidn = new Date(now.getFullYear(), now.getMonth(), now.getDate());  

// Set specificDate to a specified date at midnight.  
var specificDate = new Date("9/21/2009");  

// Compare the two dates by comparing the millisecond  
// representations.  
if (todayAtMidn.getTime() == specificDate.getTime())  

//Output: Different  

By modifying the preceding example, we can check whether a provided date is within a particular range.

// Get the current date at midnight.  
var now = new Date();  
var todayAtMidn = new Date(now.getFullYear(), now.getMonth(), now.getDate());  

// Set start/end dates to a specified date (ISO format).  
var startDate = new Date("2009-06-09T15:20:00Z");  
var endDate = new Date("2011-06-09T15:20:00Z");  

// Compare the two dates by comparing the millisecond  
// representations.  
if (todayAtMidn.getTime() > startDate.getTime() &&   
    todayAtMidn.getTime() < endDate.getTime()) {  
    document.write("Specified date is within this range.");  
else {  
    document.write("Specified date is not in this range.");  

// Output: Specified date is not in this range.  

See Also

Date Object