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]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: March 27 2003
Revised: November 19, 2003

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