Get

Get

Written

The GET command is used to retrieve a part of the current date or time. The information is constructed from the visitor's computer settings.

To properly use this feature, a new variable has to be set using a special value.

I used "thedate" as the variable name for the example. You can change it to any other name you like.

The value new Date( ) tells the browser that this variable is going to contain a specified date and/or time. It is a base setting or ground. Once that is in place, the other elements can be built on top of it.

Here is an example that shows how to print out today's date:

The result ~ Today's date is : 19 / 1 / 119

Here is a break down of what is happening in the example :

  1. a variable called thedate is being used to create a date and/or time value.
  2. JavaScript (being a builder script) gets more specified information to attach to thedate. In this case, the year, month and day.
  3. Notice the "+ 1" for the month variable? That's because JavaScript likes to start counting at 0 instead of 1. Don't ask me WHY that particular one and not the others. I don't know.
  4. Print out the day, month and year.

How about an example that shows a message relating to the time of day?

The result ~ It is morning

Here is a break down of what is happening in the example:

  1. a variable is set to collect a date/time.
  2. a variable is set to get the HOUR (0 to 23) of the day.
  3. IF the hour is less than 12 (noon), then write "Morning", else continue on.
  4. IF the hour passed 12 and is less than 17 (4 o'clock), then write "Afternoon", else continue on.
  5. IF the hour passed the first 2 tests, the hour must be greater than 17, so write "Evening".

Table of Contents

Previous
JavaScript Loops