...

View Full Version : having trouble getting a date



ckdoublenecks
09-01-2010, 11:23 PM
Someone tell me where I'm going wrong. I'm trying to print out the month/year ?


<html><head>
<SCRIPT language=JavaScript>
var dateNow = new Date();
var monthNow = dateNow.getMonth();
var yearNow = dateNow.getYear();
function MakeArray(n) {
this.length = n
return this
}
monthNames = new MakeArray(12)
monthNames[1] = "Janurary"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "Sept."
monthNames[10] = "Oct."
monthNames[11] = "Nov."
monthNames[12] = "Dec."
function customDate(oneDate) {
var theMonth = monthNames[oneDate.getMonth() +1]
}
</SCRIPT>
</head><body><script type="text/javascript">
document.write(theMonth + "/" + yearNow);
</script>
</body></html>

Old Pedant
09-01-2010, 11:32 PM
Have you tried eHarmony.com???

(Sorry, couldn't resist.)

You have NEVER CALL your customDate function.

And too much code. K.I.S.S.:


<html><head>
<script type="text/javascript">
var monthNames = [ "January","February","March","April","May","June","July",
"August","September","October","November","December" ];
var today = new Date();
var mmyy = monthName[today.getMonth()] + "/" + today.getYear();
</script>
</head>
<body>
<script type="text/javascript">document.write(mmyy);</script>
</body>
</html>

ckdoublenecks
09-02-2010, 12:21 AM
thanks for the response. You probably know a novice by the volume of code. I get two errors: unterminated string constant @ line 3 and undefined 'mmyy' @ line 8 ?

Old Pedant
09-02-2010, 12:38 AM
You are missing a " in line 3 and that then causes the problem in line 8.

And I missed the "s" on the end of monthNames in line 6.

Check the code carefully.

Note that this MIGHT give you an answer of "September/110" because of the use of getYear().

You might want to use getFullYear() to produce "September/2010". If you really need "September/10" then I'd use getFullYear() % 100.

So:


<html><head>
<script type="text/javascript">
var monthNames = [ "January","February","March","April","May","June","July",
"August","September","October","November","December" ];
var today = new Date();
var mmyy = monthNames[today.getMonth()] + "/" + today.getFullYear() % 100;
</script>
</head>
<body>
<script type="text/javascript">document.write(mmyy);</script>
</body>
</html>
where the % 100 is up to you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum