Click to See Complete Forum and Search --> : date format help needed


xdef
03-20-2003, 04:55 AM
The code below works okay but I need to change the mm/dd/yy format. I can do this with other scripts but I want to use it in the form I've placed there. I took the date part out of the clock() function to try to make it work. Im just a struggling student. HELP! If I put it back in the function how do i change the format. Any advice welcome!!

thanks....xdef




<html><title>Clock</title>
<head>

<script Language="JavaScript">
<!-- hide

var timeStr,dateStr;

function clock() {
now= new Date();
hours= now.getHours();
minutes= now.getMinutes();
seconds= now.getSeconds();
timeStr= "" + hours;
timeStr+= ((minutes < 10) ? ":0" : ":") + minutes;
timeStr+= ((seconds < 10) ? ":0" : ":") + seconds;
document.clock.time.value = timeStr;




// date
date= now.getDate();

month= now.getMonth()+1;
year= now.getYear();
dateStr= "" + month;
dateStr+= ((date < 10) ? "/0" : "/") + date;
dateStr+= "/" + year;
document.clock.date.value = dateStr;



Timer= setTimeout("clock()",1000);
}
//-->
</script>

</head>



<body bgcolor="red" onLoad="clock()">

<div align="center">

<form name="clock">
Time:
<input type="text" name="time" size="16" value="">
Date:
<input type="text" name="date" size="16" value="">
</form>
<input type='button' value='EXIT' onClick='parent.close()'><br>
</div>
</body></html>

xdef
03-20-2003, 09:51 AM
Thanks Dave. Interesting. I have played around some more after posting
and this is what i came up with (script follows).

If you try this script youll see it delivers the 0 into month; changes into the dd/mm/yy
with those minor mods youll see there; and also runs a function for correct year.

Yet still I haven't figured out to place a 0 where the day=number is. Youll see what where I mean. The clock is called from the main window by way of a pop up.

<html><title>Clock</title>
<head>

<script Language="JavaScript">
<!-- hide

var timeStr,dateStr;

function clock() {
now= new Date();
hours= now.getHours();
minutes= now.getMinutes();
seconds= now.getSeconds();
timeStr= "" + hours;
timeStr+=((minutes < 10) ? ":0" : ":") + minutes;
timeStr+=((seconds < 10) ? ":0" : ":") + seconds;
document.clock.time.value = timeStr;
Timer= setTimeout("clock()",1000);

// date
date= now.getDate();

month= now.getMonth()+1;
year= now.getYear();
dateStr="" +date +"/";
dateStr+=((month < 10) ? ":0" : ":") + month;+ month;
dateStr+= "/" + year;
document.clock.date.value = dateStr;

}

function correctYear(year) {
year = year - 0;
if (year < 70) return (2000 + year);
if (year < 1900) return (1900 + year);
return year;
}


//-->
</script>

</head>



<body bgcolor="red" text="#ffffff" onLoad="clock()">

<div align="center">
<form name="clock">
Time:
<input type="text" name="time" size="16" value="">
Date:
<input type="text" name="date" size="16" value="">
</form>
<input type='button' value='EXIT' onClick='parent.close()'><br>
</div>
</body></html>

xdef
03-20-2003, 11:47 PM
COOL!! thanks dave. Fixed.

How about this.....the correctYear function is being read in netscape and shows the year as 103. U can see I've used combined()......I also tried 2 functions at body onLoad separated by ; but this didn't work (though I see it has worked for others).

What do u think is up here?

Here is the code again....with your recommended changes completed.

<html><title>Clock</title>
<head>

<script Language="JavaScript">
<!-- hide

var timeStr,dateStr;

function clock() {
now= new Date();
hours= now.getHours();
minutes= now.getMinutes();
seconds= now.getSeconds();
timeStr= "" + hours;
timeStr+=((minutes < 10) ? ":0" : ":") + minutes;
timeStr+=((seconds < 10) ? ":0" : ":") + seconds;
document.clock.time.value = timeStr;
Timer= setTimeout("clock()",1000);

// date
date= now.getDate();

month= now.getMonth()+1;
year= now.getYear();
dateStr = ((date < 10) ? "0" : "") + date + "/";
dateStr+=((month < 10) ? ":0" : ":") + month;+ month;
dateStr+= "/" + year;
document.clock.date.value = dateStr;

}

function correctYear(year) {
year = year - 0;
if (year < 70) return (2000 + year);
if (year < 1900) return (1900 + year);
return year;
}

function combined() {
correctYear();
clock();
}


//-->
</script>

</head>



<body bgcolor="red" text="#ffffff" onLoad="combined()">

<div align="center">
<form name="clock">
Time:
<input type="text" name="time" size="16" value="">
Date:
<input type="text" name="date" size="16" value="">
</form>
<input type='button' value='EXIT' onClick='parent.close()'><br>
</div>
</body></html>

xdef
03-21-2003, 08:20 AM
a simple but igenious little fix that works like a charm for NS and IE.

thanks again
xdef