JavaScript by Example: JavaScript Core Objects. Pt. 1 | 5

JavaScript by Example: JavaScript Core Objects. Pt. 1

The Date Object

JavaScript provides the Date object for manipulating date and time.3 Like the String and Array objects, you can create as many instances as you like.


As we'll see, the Date object provides a number of methods for getting or setting specific information about the date and time. The date is based on the UNIX date starting at January 1, 1970 (in Greenwich Mean Time4 [GMT]), and doesn't support dates before that time. Figure 9.12 gives you an idea of the difference between GMT and local time. Time is measured in milliseconds (one millisecond is one thousandth of a second). Since client-side JavaScript programs run on a browser, the Date object returns times and dates that are local to the browser, not the server. Of course, if the computer is not set to the correct time, then the Date object won't produce the expected results. Figure 9.13 shows a typical date and time control panel.


Using the Date Object Methods

The Date object comes with a large number of methods (see Table 9.3) and only a prototype property. For browser versions supporting Date methods, see http://www.w3schools.com/js/js_datetime.asp.

Date object methods.

Method

What It Does

getDate

Returns the day of the month (1-31)

getDay

Returns the day of the week (0-6); 0 is Sunday, 1 is Monday, etc.

getFullYear

Returns the year with 4 digits*

getHours

Returns the hour (0-23)

getMilliseconds

Returns the millisecond*

getMinutes

Returns hours since midnight (0-23)

getMonth

Returns number of month (0-11); 0 is January, 1 is February, etc.

getSeconds

Returns the second (0-59)

getTime

Returns number of milliseconds since January 1, 1970

getTimeZoneOffset

Returns the difference in minutes between current time on local computer and UTC (Universal Coordinated Time)

getUTCDate()

Returns the day of the month*

getUTDDay()

Returns the day of the week converted to universal time*

get UTCFullYear()

Returns the year in four digits converted to universal time*

getUTCHours()

Returns the hour converted to universal time*

getUTCMilliseconds()

Returns the millisecond converted to universal time*

parse()

Converts the passed-in string date to milliseconds since January 1, 1970

setDate(value)

Sets day of the month (1-31)

setFullYear()

Sets the year as a four-digit number*

setHours()

Sets the hour within the day (0-23)

setHours(hr,min,sec,msec)

Sets hour in local or UTC time

setMilliseconds

Sets the millisecond*

setMinutes(min,sec, msec)

Sets minute in local time or UTC

setMonth(month,date)

Sets month in local time

setSeconds()

Sets the second

setTime()

Sets time from January 1, 1970, in milliseconds

setUTCdate()

Sets the day of the month in universal time

setUTCFullYear()

Sets the year as a four-digit number in universal time*

setUTCHours()

Sets the hour in universal time*

setUTCMilliseconds()

Sets the millisecond in universal time*

setUTCMinutes()

Sets the minute in universal time*

setUTCMonth()

Sets the month in universal time*

setUTCSeconds()

Sets the second in universal time*

setYear()

Sets the number of years since 1900 (00-99)

toGMTString()

Returns the date string in universal format

toLocaleString

Returns string representing date and time based on locale of computer as 10/09/99 12:43:22

toSource

Returns the source of the Date object*

toString

Returns string representing date and time

toUTCString

Returns string representing date and time as 10/09/99 12:43:22 in universal time*

UTC()

Converts comma-delimited values to milliseconds*

valueOf()

Returns the equivalence of the Date object in milliseconds*

* Starting with Netscape 4.0 and IE 4.0.

 

<html>

<head><title>Time and Date</title></head>

<body bgcolor="lightblue"><h2>Date and Time</h2>

<script language="JavaScript">

1 var now = new Date(); // Now is an instance of a Date object

document.write("<font size='+1'>");

document.write("<b>Local time:</b> " + now + "<br>");

2 var hours=now.getHours();

3 var minutes=now.getMinutes();

4 var seconds=now.getSeconds();

5 var year=now.getFullYear();

document.write("The full year is " + year +"<br>");

document.write("<b>The time is:</b> " +

hours + ":" + minutes + ":" + seconds);

document.write("</font>");

</script>

</body>

</html>

 

 
A new Date object called now is created. It contains a string: Thu Feb 6 20:02:02 PST 2003.
  1. The variable called hours is assigned the return value of the getHours() method.

  2. The variable called minutes is assigned the return value of the getMinutes() method.

  3. The variable called seconds is assigned the return value of the getSeconds() method.

  4. The variable called year is assigned the return value of the getFullYear() method, 2003. The output is shown in Figure 9.14.

 

Created: March 27 2003
Revised: November 19, 2003

URL: http://webreference.com/programming/java_core/1