Changes

JavaScript Date Object

1,691 bytes added, 18:09, 8 May 2007
Reading the Date and Time from a JavaScript Date Object
// Outputs 20 5 2007 12 0 0
 
</pre>
 
It is also possible to obtain the time in human readable form relative to a number of different time zones.
 
=== UTC Time ===
 
''Coordinated Universal Time'' (UTC) is the equavalent to ''Greenwich Mean Time'' (GMT). Using the ''toUTCString()'' method of the JavaScript Date Object will return to you the current UTC date and time. Supposing the visitor to your web page is in the United States in Eastern Standard Time (EST) and the local time is 12:00pm and the script on your web page performs the following:
 
<pre>
 
var myDate = new Date (); // Get the date of the user's system
 
document.write ("Current (UTC) Time is " + myDate.toUTCString());
 
</pre>
 
you should expect to see the following text appear because 12:00 EST is really 16:00 UTC.
 
Current (UTC) Time is Wed, 20 Jun 2007 16:00:00 GMT
 
== Finding the Time Zone Offset and Getting Local Time ==
 
It is often useful to be able to find out the time zone offset of a web site visitor. JavaScript provides the ''getTimezoneOffset()'' method which, as the name suggests, returns the number of minutes difference between the client (i.e where the browser is being run) and UTC:
 
<pre>
 
var myDate = new Date (); // Get the date of the user's system
 
document.write ("Hours Offset from UTC is " + myDate.getTimezoneOffset());
 
</pre>
 
If the visitor to your web site used Eastern Standard Time them the above would report the time offset as 240 minutes. You can, ofcourse, convert this to hours simply by dividing by 60. The following displays the time offiset as 4 hours:
 
<pre>
 
var myDate = new Date (); // Get the date of the user's system
 
document.write ("Hours Offset from UTC is " + myDate.getTimezoneOffset()/60 );
</pre>