Time, Dates & Things



These Scripts Work in IE 5.5+, Opera 8+, Google Chrome 4 & Firefox 1.5+. Although in Firefox & Google Chrome 4 the text colours work to varying degrees in some scripts.



Changables in Red
Unhangables in Blue


You put the script in the <BODY> -- </BODY> tags where you want the date to show.

Here's the code form what you can see at the top of the page I've added a couple of line breaks to make the result a little easier.

<script language="JavaScript">
var
months = new Array()
months[0] ="January"
months[1] ="February"
Etc, Etc, Etc.
months[11] ="December"

var shortMth = new Array()
shortMth[0] = "Jan,"
shortMth[1] = "Feb,"
Etc, Etc, Etc.
shortMth[11] = "Dec,"

var thisDay = new Array()
thisDay[0] = "Sunday"
thisDay[1] = "Monday"
Etc, Etc, Etc.
thisDay[6] = "Saturday"

thisDate = new Date();

mth=thisDate.getMonth()+1;

The problem is that JavaScript starts all arrays’ at zero, so that the month turns out to be one less than expected if we just use the getMonth(); e.g. 3 would return April not March. Yet the year, day and all the time elements come out as we expect them to. We can overcome the month problem by doing this mth=thisDate.getMonth()+1; then using mth as our variant as seen the code example at the top.

Why do some start at zero and some at one? If you look, you'll see that those that start at zero, can also be expressed another way; e.g. The days of the week can only be either Sun or Sunday, Mon or Monday, etc. The month could be a number 1 to 12 or it could be Jan or January, Feb or February, etc.

The reason is because of the way that an Array is constructed. All arrays’ start at zero and although the hour can be expressed as either 2300 or 11p.m. this is the exception to the rule as midnight is 0000 and therefore falls into the normal for an Array. Minutes and seconds start at zero because those are the only possible values for minutes and seconds!

var day = thisDate.getDate();
Here is an alphabetical listing that describes the date object elements are and what they do:

getDate: Returns the day of the month from the Object. These values are between 1 and 31.

getDay: Returns the days of a week. The values are between 0 (Sunday) and 6 (Saturday).

getFullYear: Returns the full year in a four digit number.

getHours: Returns the hours. These values are between 0 (Midnight or 12 A.M.) and 23 (11 P.M.).

getMilliseconds: Returns the number of milliseconds between two Veribles. e.g. Midnight Jan 1st & now.

getMinutes: Returns the minutes. These values are between 0 and 59.

getMonth: Returns the month. These values are between 0 equalling January and 11 equalling December.

getSeconds: Returns the seconds. These values are between 0 and 59.

getTime: Returns the time expressed in the number of milliseconds past midnight on January 1st at “Greenwich Mean Time” (GMT) which now also called “Coordinated Universal Time” (UTC) or just called “Universal Time” (UT) in some circles. You can easily change these into seconds, minutes or hours if you want.

getTimezoneOffset: Returns the number of minute’s difference between GMT or UTC and your local time zone. This is done in minutes because some time zones are offset by the half hour.

document.write("The current month is " + months[thisDate.getMonth()] +1; + ".")

document.write("Today is" +thisDay [thisDate.getDay()] +".")

document.write("Today's date is " + thisDate.getDate() + "/"+ mth + "/" + thisDate.getYear() + ".")

document.write("You entered this Web Page at exactly: " + thisDate.getHours() + " Hours, " + thisDate.getMinutes() +" Minutes, " + thisDate.getSeconds() +" Seconds and " + thisDate.getMilliseconds() + " Milliseconds on "+thisDay[thisDate.getDay()]+" the " +day);

This piece of code I wrote into the script is an if statement that allows us to write the st, nd, rd and th behind the days in the date in the super position.
if ((day = =1)||(day = =21)||(day = =31)){
document.write ("<small><sup>
st</sup></small>")}

else if ((day = =2)||(day = =22)){
document.write("<small><sup>
nd </sup></small>")}

else if ((day= =3)||(day= =23)){
document.write("<small><sup>
rd </sup></small> ")
}
else {
document.write("<small><sup>
th</sup></small>")
}

document.write("The number of milliseconds past midnight Greenwich Mean Time (GMT) from January 1<small><sup>st</sup></small> is "+ thisDate.getTime()+" Milliseconds.")

document.write("The number of minutes difference between GMT and your local time zone is " +thisDate.getTimezoneOffset()+ " Minutes.")
</script>

Now try this bit of code:

myMonth = new Date();
var
months= new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
document.write("
The current month is " + months[myMonth.getMonth()] + ".")

Since this code returns values starting at zero, getMonth can be now used to index our array of month names! You can use exactly the same method for the days of the week and it can also be used for the hours of the day if you want them to be AM & PM format.

Select and Copy this JavaScript
into the <body> tags in your document
where you want the script to be.


ClockMovingFastForward01_Big.gif

Well that is it. I pray that I have helped you understand how to put times and dates into your web page.

CU
Blessings to you and yours.
Gary.

 

 

BlueBar.gif


              Visitor N°
               
Free Text Counter.
Counter added 03/02/2014.

 

Top