When creating a Date object based on the computer’s (not web server’s!) internal clock, it is important to note that if someone’s clock is off by a few hours or they are in a different time zone, then the Date object will create a different times from the one created on your own computer.
It is Now
Nothing shows up! That’s because we still don’t know the methods of the Date object that let us get the information we need (i.e. Day, Month, Hour, etc).
The Date object has been created, and now we have a variable that holds the current date! To get the information we need to print out, we have to utilize some or all of the following functions:
getTime() – Number of milliseconds since 1/1/1970 @ 12:00 AM
getSeconds() – Number of seconds (0-59)
getMinutes() – Number of minutes (0-59)
getHours() – Number of hours (0-23)
getDay() – Day of the week(0-6). 0 = Sunday, … , 6 = Saturday
getDate() – Day of the month (0-31)
getMonth() – Number of month (0-11)
getFullYear() – The four digit year (1970-9999)
Now we can print out the date information. We will be using the getDate, getMonth, and getFullYear methods in this example.
It is now 1/4/2012 !
Notice that we added 1 to the month variable to correct the problem with January being 0 and December being 11. After adding 1, January will be 1, and December will be 12.
Now, instead of displaying the date we, will display the format you might see on a typical digital clock — HH:MM AM/PM (H = Hour, M = Minute).
It is now 22:13 PM
Above, we check to see if either the hours or minutes variable is less than 10. If it is, then we need to add a zero to the beginning of minutes. This is not necessary, but if it is 1:01 AM, our clock would output “1:1 AM”, which doesn’t look very nice at all!