dcsimg

The JavaScript Diaries: Part 15

By Lee Underwood

WEBINAR:
On-Demand

Application Security Testing: An Integral Part of DevOps


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.




Page 3 of 4

Previous Page
1 2 3 4
Next Page

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.

    By submitting your information, you agree that htmlgoodies.com may send you HTMLGOODIES offers via email, phone and text message, as well as email offers about other products and services that HTMLGOODIES believes may be of interest to you. HTMLGOODIES will process your information in accordance with the Quinstreet Privacy Policy.

  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date