Using the Java Script Date Object
By Stephen Bucaro
When you want to work with a date in Java Script, you need to create a Date object.
The code shown below creates a Date object and uses it to display today's date in a message box.
var today = new Date();
alert(today);
• If you want to follow along and get some hands-on experience,
create a new file in Windows Notepad or your favorite ASCII text editor and type in
the html code shown below.
<html>
<body>
<script type="text/javascript">
... JavaScript code here ...
</script>
</body>
</html>
Save the file with the extension .htm. For each example, replace the text
"... JavaScript code here ..." with the example code. Resave the file, then double-click
on the file name to execute the example.
If you're using Internet Explorer, you'll receive the message "Internet Explorer has
restricted this webpage from running scripts". Right-click on the message and in the
popup menu that appears, select "Allow Blocked Content...". This is how Microsoft
Expects to protect Windows, by making it annoying and unusable.
The Date object constructor reads the computers internal clock. The internal value in
a Date object is the number of milliseconds since midnight January 1, 1970 in GMT
(Greenwich Mean Time).
• In the old days, we used to call this "Greenwich Meridian Time",
because it's the time at Earth's 0 degrees longitude line, which happens to pass through
the city of Greenwich, England, where they have the over 325 year old Greenwich Observatory.
But apparently some people couldn't figure out what a "Meridian" was, so they changed it to
"Mean". Today, most people use UTC (Coordinated Universal Time), which is GMT based on
an atomic clock and expressed as a 24 hour clock.
The date in the Date object is a static date, it will not change over time, you need
to update it programmatically. When you display the contents of a Date object, JavaScript
converts it to the local time zone as configured in your PC's Control Panel.
Getting Past or Future Dates
You can get a date from the past or future by passing a date, formatted as yyyy,mm,dd
to the Date object, as shown below.
var pastDate = new Date(2007,11,18);
alert(pastDate);
• Note: The Date object uses a zero-based month, with January being month 0.
In other words, subtract 1 from the number of the month that you pass to the Date object.
You can also get a date from the past or future by adding or subtracting the required
number of milliseconds from the current date, as shown below.
var today = new Date();
var todayMS = today.getTime();
var nextWeekMS = todayMS + (60 * 60 * 24 * 7 * 1000);
var nextWeek = new Date(nextWeekMS);
alert(nextWeek);
In this example, after we create a Date object, we use it's getTime method to
retrieve it's contents (the number of milliseconds since midnight January 1, 1970).
Then we add a weeks worth of milliseconds (calculated as shown below) to that value.
60 seconds in a minute * 60 minutes in an hour * 24 hours in a day * 7 days in a week *
1000 milliseconds in a second.
Then we create a new Date object, named nextWeek, passing the new milliseconds
value to its constructor. When we display this new Date object, it will contain the
date one week from now, including the day of the week.
|