spacer

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

home / programming / javascript / diaries / 15

[previous] [next]

Technical Lead
Thomson Reuters (Markets) LLC
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?


The JavaScript Diaries: Part 15 - The Date Object

Each of the methods listed is self-explanatory. Let's look now at how to use some of them.

To display the date, you could use the following function:

It would display as: (Remember, the date is only as accurate as the settings in your user's computer system.)

Let's take a look at what we did:

  • First, we created a function named displayDate()
  • Then, we declared the variable thisDate. We created a new instance of the Date() object and assigned it to the thisDate variable.
  • Next, we declared the variable curDay and gave it the value of the current day of the month, which is derived from the value of the variable thisDate, using the dot operator in conjunction with the getDate() method.
  • We then declared the variable curMonth and gave it the value of the current month, which is derived from the value of the variable thisDate, using the dot operator in conjunction with the getMonth() method..
    • Note that we added "1" to the value of the method before assigning it to the month variable. This is because months are counted, starting with "0", i.e. January = "0", December ="11". By adding the "1", it now becomes the current month. (We would need to do it with the day of the week also, if we were using it.)
  • Then, we declared the variable curYear and gave it the value of the current year, which is derived from the value of the variable thisDate, using the dot operator in conjunction with the getFullYear method.
  • Finally, we created a string to print out the current date, using a document.write statement.
  • In order to display the date on the page, we placed a call to the function in the place we wanted to display the date.

Sounds a bit complicated but it's not. Once you get the hang of it, it's quite simple. Let's apply our knowledge of arrays and see how to spice it up:

It would display as:

See? It's actually quite simple. First we created a set of parallel arrays, one for the month and one for the day of the week. Since the array indexes are already assigned to the given months and days, we don't need to add "1" to the number. Next, we declared the variables and assigned values to them. Finally, we used a document.write to format and display the date.

Let's look at some other ways to use the Date() object methods.

home / programming / javascript / diaries / 15

[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

URL: