Javascript Date
The methods getDate(), getDay(), getMonth(), and getFullYear() allow us to
retrieve date values from inside a Date object.
The setDate(), setMonth(), and setFullYear() methods allow us to set the
date values of an existing Date object.
The getHours(), getMinutes(), getSeconds(), and getMilliseconds() methods retrieve the time values in a Date object. The setHours(), setMinutes(), setSeconds(), and setMilliseconds() methods allow us to set time values of an existing Date object. One thing that we didn't cover in that chapter was the idea that the time depends on your location around the world. For example, imagine you have a chat room on your website and want to organize a chat for a certain date and time. Simply stating 15:30 is not good enough if your website attracts international visitors. The time 15:30 could be Eastern Standard Time, Pacific Standard Time, the time in the United Kingdom, or even the time in Kuala Lumpur. You could of course say 15:30 EST and let your visitors work out what that means, but even that isn't foolproof. There is an EST in Australia as well as in the U.S. var localTime = new Date();
This creates a new Date object and
initializes it to the current date and time based on the client computer's
clock. (Note that in fact the Date object simply stores
the number of milliseconds between the date and time on your computer's clock
and midnight UTC time on the 1st of January 1970.)
Within the body of the page we have seven more script blocks that
use the three world time methods we looked at earlier. Note that some of them
are enclosed in an if statement, for example
if (localTime.toLocaleTimeString) { document.write(localTime.toLocaleTimeString()) }
This checks to see if the browser supports that method and only
makes use of it if it does. Older browsers don't support all of the date/time
methods so doing this prevents ugly errors.
In the following line
document.write(localTime.toUTCString());
we write the string returned by the toUTCString() method to the page. This converts the date and
time stored inside the localTime Date object to the equivalent UTC date and time.
|
Home »
JavaScript
» javascript Date, Time
javascript Date, Time
PMA10:30
Categories: JavaScript