The script of Fig. 11.9 demonstrates many of the local time zone methods in Fig. 11.8. Line 12 creates a new Date object. The new operator allocates the memory for the Date object. The empty parentheses indicate a call to the Date object’s constructor with no arguments. A constructor is an initializer method for an object. Constructors are called automatically when an object is allocated with new. The Date constructor with no arguments initializes the Date object with the local computer’s current date and time.
Lines 16–19 demonstrate the methods toString, toLocaleString, toUTCString and valueOf. Note that method valueOf returns a large integer value representing the total number of milliseconds between midnight, January 1, 1970, and the date and time stored in Date object current.
Lines 23–32 demonstrate the Date object’s get methods for the local time zone. Note that method getFullYear returns the year as a four-digit number. Note as well that method getTimeZoneOffset returns the difference in minutes between the local time zone and UTC time (i.e., a difference of four hours in our time zone when this example was executed).
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<!-- Fig. 11.9: DateTime.html -->
<!-- Date and time methods of the Date object. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<title>Date and Time Methods</title>
var current = new Date();
"<h1>String representations and valueOf</h1>" );
document.writeln( "toString: " + current.toString() +
"<br />toLocaleString: " + current.toLocaleString() +
"<br />toUTCString: " + current.toUTCString() +
"<br />valueOf: " + current.valueOf() );
"<h1>Get methods for local time zone</h1>" );
document.writeln( "getDate: " + current.getDate() +
"<br />getDay: " + current.getDay() +
"<br />getMonth: " + current.getMonth() +
"<br />getFullYear: " + current.getFullYear() +
"<br />getTime: " + current.getTime() +
"<br />getHours: " + current.getHours() +
"<br />getMinutes: " + current.getMinutes() +
"<br />getSeconds: " + current.getSeconds() +
"<br />getMilliseconds: " + current.getMilliseconds() +
"<br />getTimezoneOffset: " + current.getTimezoneOffset() )
"<h1>Specifying arguments for a new Date</h1>" );
var anotherDate = new Date( 2007, 2, 18, 1, 5, 0, 0 );
document.writeln( "Date: " + anotherDate );
document.writeln( "<h1>Set methods for local time zone</h1>" );
anotherDate.setDate( 31 );
anotherDate.setMonth( 11 );
anotherDate.setFullYear( 2007 );
anotherDate.setHours( 23 );
anotherDate.setMinutes( 59 );
anotherDate.setSeconds( 59 );
document.writeln( "Modified date: " + anotherDate );
Fig. 11.9 | Date and time methods of the Date object.
Line 36 demonstrates creating a new Date object and supplying arguments to the Date constructor for year, month, date, hours, minutes, seconds and milliseconds. Note that the hours, minutes, seconds and milliseconds arguments are all optional. If any one of these arguments is not specified, a zero is supplied in its place. For the hours, minutes and seconds arguments, if the argument to the right of any of these arguments is specified, it too must be specified (e.g., if the minutes argument is specified, the hours argument must be speci-fied; if the milliseconds argument is specified, all the arguments must be specified).
Lines 40–45 demonstrate the Date object set methods for the local time zone. Date objects represent the month internally as an integer from 0 to 11. These values are off by one from what you might expect (i.e., 1 for January, 2 for February, …, and 12 for December). When creating a Date object, you must specify 0 to indicate January, 1 to indicate February, …, and 11 to indicate December.
The Date object provides two other methods that can be called without creating a new Date object—Date.parse and Date.UTC. Method Date.parse receives as its argument a string representing a date and time, and returns the number of milliseconds between mid-night, January 1, 1970, and the specified date and time. This value can be converted to a Date object with the statement
var theDate = new Date( numberOfMilliseconds );
which passes to the Date constructor the number of milliseconds since midnight, January 1, 1970, for the Date object.
Method parse converts the string using the following rules:
Short dates can be specified in the form MM-DD-YY, MM-DD-YYYY, MM/DD/YY or MM/ DD/YYYY. The month and day are not required to be two digits.
Long dates that specify the complete month name (e.g., “January”), date and year can specify the month, date and year in any order.
Text in parentheses within the string is treated as a comment and ignored. Com-mas and white-space characters are treated as delimiters.
All month and day names must have at least two characters. The names are not required to be unique. If the names are identical, the name is resolved as the last match (e.g., “Ju” represents “July” rather than “June”).
If the name of the day of the week is supplied, it is ignored.
All standard time zones (e.g., EST for Eastern Standard Time), Coordinated Universal Time (UTC) and Greenwich Mean Time (GMT) are recognized.
When specifying hours, minutes and seconds, separate each by colons.
When using a 24-hour-clock format, “PM” should not be used for times after 12 noon.
Date method UTC returns the number of milliseconds between midnight, January 1, 1970, and the date and time specified as its arguments. The arguments to the UTC method include the required year, month and date, and the optional hours, minutes, seconds and mil-liseconds. If any of the hours, minutes, seconds or milliseconds arguments is not specified, a zero is supplied in its place. For the hours, minutes and seconds arguments, if the argument to the right of any of these arguments in the argument list is specified, that argument must also be specified (e.g., if the minutes argument is specified, the hours argument must be specified; if the milliseconds argument is specified, all the arguments must be specified). As with the result of Date.parse, the result of Date.UTC can be converted to a Date object by creating a new Date object with the result of Date.UTC as its argument.
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.