Share via


JScript Date Object

The JScript Date object can be used to represent arbitrary dates and times, to get the current system date, and to calculate differences between dates. It has several predefined properties and methods. The Date object stores a day of the week; a month, day, and year; and a time in hours, minutes, seconds, and milliseconds. This information is based on the number of milliseconds since January 1, 1970, 00:00:00.000 Coordinated Universal Time (UTC), formerly known as Greenwich Mean Time. JScript can handle dates that are in the approximate range from 250,000 B.C. to 255,000 A.D., although some formatting functionality is supported only for dates in the range 0 A.D. through 9999 A.D.

Creating a Date Object

To create a new Date object, use the new operator. The following example calculates the number of days that have passed and the number of days that remain for the current year.

// Get the current date and read the year.
var today : Date = new Date();
// The getYear method should not be used. Always use getFullYear.
var thisYear : int = today.getFullYear();

// Create two new dates, one for January first of the current year,
// and one for January first of next year. The months are numbered
// starting with zero.
var firstOfYear : Date = new Date(thisYear,0,1);
var firstOfNextYear : Date = new Date(thisYear+1,0,1);

// Calculate the time difference (in milliseconds) and 
// convert the differnce to days.
const millisecondsToDays = 1/(1000*60*60*24);
var daysPast : double = (today - firstOfYear)*millisecondsToDays;
var daysToGo : double = (firstOfNextYear - today)*millisecondsToDays;

// Display the information.
print("Today is: "+today+".");
print("Days since first of the year: "+Math.floor(daysPast));
print("Days until the end of the year: "+Math.ceil(daysToGo));

The output of this program is similar to this:

Today is: Sun Apr 1 09:00:00 PDT 2001.
Days since first of the year: 90
Days until the end of the year: 275

See Also

Reference

Date Object

Other Resources

Intrinsic Objects