spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java_core / 1 To page 1To page 2To page 3To page 4current pageTo page 6To page 7
[previous] [next]

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


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.


24-hour world time zones map with current time. Courtesy of http://www.worldtimezone.com/index24.html.

 

The computer's date and time settings.

If no arguments are passed to the Date object constructor, it returns the local date and time (based on the accuracy of the clock on your client machine). There are five formats that can be passed as arguments when creating a Date object.

 

 
 

 

var Date = new Date(); // The new constructor returns a Date object.

var Date = new Date("July 4, 2004, 6:25:22");

var Date = new Date("July 4, 2004");

var Date = new Date(2004, 7, 4, 6, 25, 22);

var Date = new Date(2004, 7, 4);

var Date = new Date(Milliseconds);

 

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.

 

home / programming / java_core / 1 To page 1To page 2To page 3To page 4current pageTo page 6To page 7
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: March 27 2003
Revised: November 19, 2003

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