Working with dates
var d = new Date();
This returns an instance of the Date object in the variable d using the current date and time. The Date object includes numerous methods and properties for accessing and manipulating its value. The following list includes methods for accessing date values:
- getDate(): Returns the day of the month.
- getDay(): Returns the day of the week. The week begins with Sunday (0 - 6).
- getFullYear(): Returns the four-digit year.
- getMonth(): Returns the month.
- getYear(): Returns the two-digit year.
- getUTCDate(): Returns the day of the month according to the Coordinated Universal Time (UTC).
- getUTCMonth(): Returns the month according to the UTC (0 - 11).
- getUTCFullYear(): Returns the four-digit year according to the UTC.
Note: Wikipedia defines UTC as a high-precision atomic time standard which approximately tracks Universal Time (UT).
You are not restricted to working with the current date. The Date object may be initialized with a value passed to it, like this:
var d = new Date("date value");
Using this approach, we could alter the previous example to use a specific date. Listing C presents a simple way to discover the day of the week for a given value. The code produces the following output:
Today is: Wednesday 4/15/1979
UTC is: Wednesday 4/15/1979
Actually, there are four approaches to creating a Date object instance:
var d = new Date();
var d = new Date('July 4, 1976');
var d = new Date(7, 4, 1976);
var d = new Date(7, 4, 1976, 12,00,00);
We've covered the first two (notice that apostrophes or parentheses may be used). The final two use individual integer parameters using the following format (the time is optional):
var d = new Date(month, day, year, hour, minutes, seconds);
Var d1 = new Date();
var d2 = new Date("7/4/1976");
There are more set methods for populating the various properties of the Date object, but let's cover time before discussing them.
Working with time
Along with the date components, the Date object stores time information as well. The following methods provide access to a Date object's time information:
- getHours(): Returns the hour portion of the time.
- getMinutes(): Returns the minutes portion of the time.
- getSeconds(): Returns the seconds portion of the time.
- getMilliseconds(): Returns the milliseconds portion of the time.
- getTime(): Returns the number of milliseconds since midnight January 1, 1970.
- getTimezoneOffset(): Returns the difference in minutes between local time and Greenwich Mean Time (GMT).
- getUTCHours(): Returns the hour portion of the time according to UTC.
- getUTCMinutes(): Returns the minutes portion of the time according to UTC.
- getUTCSeconds(): Returns the seconds portion of the time according to UTC.
- getUTCMilliseconds(): Returns the milliseconds portion of the time according to UTC.
var d = new Date();
document.write(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() +
":" + d.getMilliseconds());
It displays the following output:
The second value is a bit odd since it displays the number of milliseconds since January 1, 1970 to the value stored in the referenced Date object. It comes in handy when finding the difference between two values. As with date values, a setTime method is available as well:
Var dt1 = new Date();
var dt2 = new Date(1970, 4, 15);
Like the setTime, setDate, and setMilliseconds methods, there are methods to populate all portions of a Date object. This includes the following:
These methods allow you to easily reset a date property by passing in its new value. It's good to be able to work with and display dates, but there will be times when you need to perform calculations with dates and so forth.
Finding the difference between two values
var d1 = new Date();
var d2 = new Date(2006, 6, 7);
var day = 1000*60*60*24;
var diff = Math.ceil((d2.getTime()-d1.getTime())/(day));
document.write("Days until vacation: " + diff);
The various properties of a Date value may be increased or decreased by adding or subtracting the necessary values via the appropriate property. For example, if you want to increase the value by one month, you would add one to the month property. The example in Listing D displays difference values for yesterday and tomorrow for the previous script. The following output is displayed:
Days until vacation: 50
Tomorrow it will be 49 days until vacation.
Yesterday, it was 51 days until vacation.
The time has come
Miss a column?
Check out the Web Development Zone archive, and catch up on the most recent editions of Tony Patton's column.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.
Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a production environment on a daily basis.