...

View Full Version : Date & Time JavaScript Help



xhtml205
04-30-2008, 07:01 PM
I cannot get my JavaScript code to work. Does anyone know what I am doing wrong with the following code:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to my Web Page</h1>
<script type="text/javascript">
/* <![CDATA[ */
var dateObject = new Date();
var greeting = "";
var curTime = "";
var minuteValue = dateObject.getMinutes();
var hourValue = dateObject.getHours();
if (minuteValue < 10) minuteValue = "0" + minuteValue;
if(hourValue < 12) { greeting = "<p>Good morning! " curTime = hourValue + ":" + minuteValue + " AM"; }
else if (hourValue == 12) { greeting = "<p>Good afternoon! "; + minute Value + " PM"; }
else if (hourValue == 17) { greeting = "<p>Good afternoon!" cutTime = (hourValue-12) + ":" + minuteValue + " PM" }
else { greeting = "<p>Good evening! " curTime = (hourValue-12) + ":" + minuteValue + " PM" }
var dayArray = newArray("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monthArray = newArray("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var day = dateObject.getDay();
var month = dateObject.getMonth();
document.write("<p>"+ greeting + "It is " + curTime + " on " + dayArray[day] + ", " + monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear() + ".</p>");
/* ]]> */
</script>
</body>
</html>

~xhtml205

Philip M
04-30-2008, 07:22 PM
A good many errors - missing semi-colons, cutTime, errors with == and <=


<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to my Web Page</h1>
<script type="text/javascript">
/* <![CDATA[ */
var dateObject = new Date();
var greeting = "";
var curTime = "";
var minuteValue = dateObject.getMinutes();
var hourValue = dateObject.getHours() ;
if (minuteValue < 10) minuteValue = "0" + minuteValue;
if(hourValue < 12) { greeting = "<p>Good morning! "; curTime = hourValue + ":" + minuteValue + " AM"};
else if (hourValue >= 12 && hourValue <=17) { greeting = "<p>Good afternoon! "; curTime = (hourValue - 12) + ":" + minuteValue + " PM"}
else { greeting = "<p>Good evening! "; curTime = (hourValue-12) + ":" + minuteValue + " PM" }

var dayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var day = dateObject.getDay();
var month = dateObject.getMonth();
document.write("<p>"+ greeting + "It is " + curTime + " on " + dayArray[day] + ", " + monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear() + ".</p>");
/* ]]> */
</script>
</body>
</html>

xhtml205
04-30-2008, 07:35 PM
Hi Phillip M,

I made the changes of cutTime and added some semi-colons, however, I am still not getting it to work. Here is what my code looks like now:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to my Web Page</h1>
<script type="text/javascript">
/* <![CDATA[ */
var dateObject = new Date();
var greeting = "";
var curTime = "";
var minuteValue = dateObject.getMinutes();
var hourValue = dateObject.getHours();
if (minuteValue < 10) minuteValue = "0" + minuteValue;
if(hourValue < 12) { greeting = "<p>Good morning! " curTime = hourValue + ":" + minuteValue + " AM"; }
else if (hourValue == 12) { greeting = "<p>Good afternoon! "; + minute Value + " PM"; }
else if (hourValue == 17) { greeting = "<p>Good afternoon!" curTime = (hourValue-12) + ":" + minuteValue + " PM"; }
else { greeting = "<p>Good evening! " curTime = (hourValue-12) + ":" + minuteValue + " PM"; }
var dayArray = newArray("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monthArray = newArray("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var day = dateObject.getDay();
var month = dateObject.getMonth();
document.write("<p>"+ greeting + "It is " + curTime + " on " + dayArray[day] + ", " + monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear() + ".</p>");
/* ]]> */
</script>
</body>
</html>

Philip M
04-30-2008, 07:37 PM
See above. I corrected a couple of typos. It works fine now.

Check out what is wrong here:

else if (hourValue == 17) { greeting = "<p>Good afternoon!" ; curTime = (hourValue-12) + ":" + minuteValue + " PM"; }
else { greeting = "<p>Good evening! " ; curTime = (hourValue-12) + ":" + minuteValue + " PM"; }

xhtml205
04-30-2008, 07:37 PM
Oh, I see. You have made the changes for me. Thank you for helping me! Now I shall go and study the code.

Again thanks.

~xhtml205

Philip M
04-30-2008, 08:04 PM
In fact it would be better/simpler to arrange your tests in reverse time order:-

if (hourValue > 16) { ....... Evening
else if (hourValue > 11) { ....... Afternoon
else { ...... Morning

whyght mic
06-28-2011, 01:47 PM
One Two Three Four Five

Philip M
06-28-2011, 05:16 PM
Change the code as follows:-


if (hourValue < 12) { greeting = "<p>Good morning! "; curTime = hourValue + ":" + minuteValue + " AM"};
if (hourValue == 12) { greeting = "<p>Good afternoon! "; curTime = hourValue + ":" + minuteValue + " PM"}
if (hourValue > 12 && hourValue <=17) { greeting = "<p>Good afternoon! "; curTime = (hourValue - 12) + ":" + minuteValue + " PM"}
if (hourValue >17) { greeting = "<p>Good evening! "; curTime = (hourValue-12) + ":" + minuteValue + " PM" }

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

whyght mic
06-28-2011, 06:49 PM
One Two Three Four Five

Philip M
06-28-2011, 08:13 PM
Sorry, it works for me. It is not possible for the script to display AM when the hourValue is 12.

Try adding the temporary line hourValue = 12 to set the hour to 12 for testing purposes regardless of the actual time. Then what I get is

Good afternoon! It is 12:11 PM on Tuesday, June 28, 2011.

whyght mic
06-28-2011, 09:51 PM
Awesome

Philip M
06-29-2011, 07:48 AM
The fault, dear Brutus, lies not in the stars but ........ The hour after midnight is 0, not 12. In Post#7 you asked for a correction for 12:00PM, not 12:00 AM. And I have to say that you ought to be able to make that simple correction yourself following the code I showed you.

Simply add another line:

if (hourValue >17) { greeting = "<p>Good evening! ";
curTime = (hourValue-12) + ":" + minuteValue + " PM" }
if (hourValue == 0 ) { greeting = "<p>Good morning! ";
curTime = 12 + ":" + minuteValue + " AM"};

This is not a very good script (I did not write it - I simply corrected the original OP's errors) and I expect you realise that it is not a clock. It will only display the time that the user entered the site, and remain at that time throughout.

whyght mic
06-29-2011, 10:44 AM
One Two Three Four Five



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum