Loops << Get >> NoScript
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.

The different types of GET are :

getFullYear( )
getMonth( )
getDate( )
getDay( )
getHours( )
getMinutes( )
getSeconds( )
The year
The month
The day of the month
The day of the week
The hour
The minutes
The seconds
4 digits
0 to 11
1 to 31
1 to 7
0 to 23
1 to 60
1 to 60

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

var thedate = new Date( );

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 :

var thedate = new Date( );
var theyear = thedate.getYear( );
var themonth = thedate.getMonth( ) + 1;
var theday = thedate.getDate( );

document.write("Today's date is : " + theday + " / " + themonth + " / " + theyear);
The result ~

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?

var thetime = new Date( );
var thehour = thetime.getHours( );

if (thehour < 12) {
document.write("It is morning");
} else if (thehour < 17) {
document.write("It is afternoon");
} else {
document.write("It is evening");
The result ~

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".
Loops << Get >> NoScript

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads