Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question about time

Status
Not open for further replies.

BigDoug

IS-IT--Management
Feb 20, 2002
49
US
I have a script that shows the time on the status bar, but I want the time to show in US standard and not the European standard which it is. I need it to show like 9/01/2002 instead of 1/9/2002 for September 1st, 2002. Can someone point me in the right direction? Here is the code:

<SCRIPT language=JavaScript>
<!--
var timeStr, dateStr;
server = new Date();
client = new Date();

delta= server.getTime() - client.getTime();

function clock() {
now=new Date();
now_server=now.getTime() + delta;
now.setTime(now_server);
//time
hours=now.getHours();
minutes=now.getMinutes();
seconds=now.getSeconds();
timeStr=''+hours;
timeStr+=((minutes<10) ? ':0':':')+minutes;
timeStr+=((seconds<10) ? ':0':':')+seconds;

//date
month=now.getMonth()+1;
date=now.getDate();
year=now.getYear();
if (year<1000) {year = year+1900};
dateStr=''+((date<10) ? '0':'')+date;
dateStr+=((month<10) ? '.0':'.')+month;
dateStr+='.'+year
window.status='Current Time: '+timeStr+' '+dateStr+' ';
Timer=setTimeout('clock()',1000);
}
clock();
//-->
</SCRIPT>
Do me justice, O God, and fight my fight against a faithless people; from the deceitful and impious man rescue me.

Psalm 43:1

 
Your code seems to claim in error that it is getting the time from the server initially, where it is actually only getting the time from the client twice in a row. The following should help you see what the code is doing a little better than the original code you had, too.

function clock()
{
var now=new Date();

//time
hours=now.getHours();
minutes=now.getMinutes();
seconds=now.getSeconds();

if (hours < 10) hours = '0' + hours;
if (minutes < 10) minutes = '0' + minutes;
if (seconds < 10) seconds = '0' + seconds;

var timeStr=hours + ':' + minutes + ':' + seconds;

//date
month=now.getMonth()+1;
date=now.getDate();
year=now.getFullYear();

if (month < 10) month = '0' + month;
if (date < 10) date = '0' + date;

var dateStr= month + '/' + date + '/' + year;

window.status='Current Time: '+timeStr+' '+dateStr+' ';
}

setInterval('clock()', 1000);
 
Thank you! Worked exactly as I needed it to. Do me justice, O God, and fight my fight against a faithless people; from the deceitful and impious man rescue me.

Psalm 43:1

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top