Sullivan Market

a web based full-circle development system



My JavaScript Tutorials - Display Date & Time

Here's the JavaScript to Display the Day of the Week and the Month:

<script language="JavaScript">
<!--

{
// Get the current date
var today = new Date();

// Get the current month
var month = today.getMonth();
switch (month) {
case 0 :
displayMonth = "January"
break
case 1 :
displayMonth = "February"
break
case 2 :
displayMonth = "March"
break
case 3 :
displayMonth = "April"
break
case 4 :
displayMonth = "May"
break
case 5 :
displayMonth = "June"
break
case 6 :
displayMonth = "July"
break
case 7 :
displayMonth = "August"
break
case 8 :
displayMonth = "September"
break
case 9 :
displayMonth = "October"
break
case 10 :
displayMonth = "November"
break
case 11 :
displayMonth = "December"
break

default: displayMonth = "INVALID"
}

var day = today.getDay();

// Attach a display name to the day number

switch (day) {
case 0 :
displayDay = "Sunday"
break
case 1 :
displayDay = "Monday"
break
case 2 :
displayDay = "Tuesday"
break
case 3 :
displayDay = "Wednesday"
break
case 4 :
displayDay = "Thursday"
break
case 5 :
displayDay = "Friday"
break
case 6 :
displayDay = "Saturday"
break

default: displayDay = "INVALID"
}
}


// -->
</script>

Here's the JavaScript to Display the Date and Time:

<script language="JavaScript">
<!--
var today = new Date()
var hours = today.getHours()
var minutes = today.getMinutes()
var greeting
var ampm
var military_hrs = hours

// We consider one in the morning until just before noon "morning"
if (hours <= 11) {
greeting = "Good morning!"
ampm="a.m."

// JavaScript reports midnight as 0, so change it to 12
// for display purposes.

if (hours == 0) {
hours = 12
}
}
// We consider noon until five o'clock "afternoon"
else if (hours > 11 && hours < 17) {
greeting = "Good afternoon!"
ampm="p.m."

// We don't want to see military time, so subtract 12
if (hours > 12) {
hours-=12
}
}
// We consider five until eight "evening"
else if (hours > 16 && hours < 21) {
greeting = "Good evening!"
ampm="p.m."
hours-=12
}
// We consider nine o'clock until midnight "night"
else if (hours > 20) {
greeting = "Good night!"
ampm="p.m."
hours-=12
}

// We want the minutes to display with "0" in front of them if
// they're single-digit. (For example, rather than 1:4 p.m.,
// we want to see 1:04 p.m.

if (minutes < 10) {
minutes = "0" + minutes
}

// -->
</script>

Here's the JavaScript to Display the Date and Time in Both IE and Netscape:

<html>
<head></head>
<body leftMargin=0 background=common/images/misc/bkgrnd_tile.gif topMargin=0
marginheight="0" marginwidth="0">

<script language="JavaScript" src="date.js"></script>

<script language="JavaScript" src="date_time.js"></script>
<script>
if (navigator.appName.indexOf("Microsoft")>=0) {
document.write(greeting + "\nIt's " + hours + ":" + minutes + " " + ampm + " on ");
document.writeln(displayDay + ", " + displayMonth + " " + today.getDate() + ", " + today.getYear() + ".");
}
else {
document.write(greeting + "\nIt's " + hours + ":" + minutes + " " + ampm + " on ");
document.writeln(displayDay + ", " + displayMonth + " " + today.getDate() + ", " + (today.getYear()+1900) + ".");
}
</script>
</body>
</html>

Here is an example:



Note:: Make sure that, for this example, you place all the files within the same folder.

Back To JavaScript Tutorials

©2012 SullivanMarket.com