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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DATE AND TIME 1

Status
Not open for further replies.

SPYDERIX

Technical User
Jan 11, 2002
1,899
0
0
CA
Hi,

I've been searching the internet to find the easiest script, I have found variations but not a direct match.

Can someone write me a simple script to show the TIME AND DATE from the users computer and in 12 hour time.

This is the exact format I want it to be in:
Friday May 31, 2002 - 7:30 AM
and it has to be white.

Can anyone help me with this?

Thanks!
colorado.gif
 
this is one I use for the date - sure it wouldnt be hard to add the time aswell:

Code:
<SCRIPT LANGUAGE=&quot;JAVASCRIPT&quot;>
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
  year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym < 10)
  daym=&quot;0&quot;+daym
var dayarray=new Array(&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;)
var montharray=new Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;)
document.write(&quot;<P CLASS=date>&quot;+dayarray[day]+&quot;, &quot;+daym+&quot; &quot;+montharray[month]+&quot; &quot;+year+&quot; </P>&quot;)
</SCRIPT>
Tony
 
Hey thas my script...lol...
Umm, if you want the white font, you have to do it this way:
document.write(&quot;<font color='white'><h6>&quot;+dayarray[day]+&quot;, &quot;+daym+&quot; &quot;+montharray[month]+&quot; &quot;+year+&quot;</h6></font>&quot;)

The <font color=white><h6> tag will make the document.write display that info in that color and font size, however if you wanna do it the way FesterSXS did, you need to make an CSS sheet and call it here, like he did with the <p class=&quot;date&quot;> tag...ok?? I have not failed; I merely found 100,000 different ways of not succeding...
 
I have a script that does what you see in the upper left corner of If you like that I can send it to you.
E-mail me at webmaster@chaindlk.com if you want it!
 
how about this one

<script language=&quot;JavaScript&quot;>
var date=new Date();
document.write(&quot;<BR>&quot;);
if (date.getHours >= 12)
{ MorNit = &quot;PM&quot; }else{ MorNit = &quot;AM&quot; }
if (date.getMinutes() <= 9) { (date.getMinutes += &quot;0 &quot;); }
mon=[&quot;Jan&quot;,&quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;Aug&quot;, &quot;Sept&quot;, &quot;Oct&quot;, &quot;Nov&quot;, &quot;Dec&quot;];
monN = (date.getMonth());
monN = (mon[date.getMonth()]);
day=[&quot;Sunday&quot;,&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;];
dayN = (date.getDay());
dayN = (day[date.getDay()]);
document.write(dayN+ &quot; &quot; +monN+ &quot; &quot; +date.getDate()+ &quot;,&quot; +date.getYear()+ &quot;-&quot; +date.getHours()+ &quot;:&quot; +date.getMinutes()+ MorNit);
</script> provide tools to let people become their best.
 
onpnt,

Yours is the best so far, however can you make it dynamic? I want it to be dynamic without seconds though.

Thanks! :)
colorado.gif
 
I actually tried for a minute to get that one dynamic and seeing as I already had one from something else I gave up pretty quick. I reformatted it to what you wanted though.
Still fairly short as well

<SCRIPT language=&quot;JavaScript&quot;>
<!--
function startclock() {
var thetime=new Date();
mon=[&quot;Jan&quot;,&quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;Aug&quot;, &quot;Sept&quot;, &quot;Oct&quot;, &quot;Nov&quot;, &quot;Dec&quot;];
monN = (thetime.getMonth());
monN = (mon[thetime.getMonth()]);
day=[&quot;Sunday&quot;,&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;];
dayN = (thetime.getDay());
dayN = (day[thetime.getDay()]);
var nday=thetime.getDate();
var nyear=thetime.getYear();
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var AorP=&quot; &quot;;
if (nhours>=12) { AorP=&quot;P.M.&quot;; } else { AorP=&quot;A.M.&quot;; }
if (nhours>=13) { nhours-=12; } if (nhours==0) { nhours=12; }
if (nsecn<10) { nsecn=&quot;0&quot;+nsecn; } if (nmins<10){ nmins=&quot;0&quot;+nmins; }
window.status=monN+&quot; &quot;+nday+&quot;, &quot;+dayN+&quot; &quot;+nyear+&quot; &quot;+nhours+&quot;:&quot;+nmins+&quot; &quot;+AorP;
setTimeout('startclock()',1000);
}
//-->
</SCRIPT>
</HEAD>
<BODY onLoad=&quot;startclock()&quot;>
</BODY>
</HTML> provide tools to let people become their best.
 
with a small addition you can use the innerHTML property to modify the actually page with a clock:
Code:
<HTML>
<HEAD>
<SCRIPT language=&quot;JavaScript&quot;>
<!--
function startclock() {
var thetime=new Date();
mon=[&quot;Jan&quot;,&quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;Aug&quot;, &quot;Sept&quot;, &quot;Oct&quot;, &quot;Nov&quot;, &quot;Dec&quot;]; 
monN = (thetime.getMonth()); 
monN = (mon[thetime.getMonth()]); 
day=[&quot;Sunday&quot;,&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;]; 
dayN = (thetime.getDay()); 
dayN = (day[thetime.getDay()]);
var nday=thetime.getDate();
var nyear=thetime.getYear();
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var AorP=&quot; &quot;;
if (nhours>=12) { AorP=&quot;P.M.&quot;; } else { AorP=&quot;A.M.&quot;; }
if (nhours>=13) { nhours-=12;  } if (nhours==0) { nhours=12; }
if (nsecn<10)   { nsecn=&quot;0&quot;+nsecn; } if (nmins<10){ nmins=&quot;0&quot;+nmins; }
var nowStr=monN+&quot; &quot;+nday+&quot;, &quot;+dayN+&quot; &quot;+nyear+&quot; &quot;+nhours+&quot;:&quot;+nmins+&quot; &quot;+AorP;
window.status=nowStr
myclock.innerHTML=nowStr;

setTimeout('startclock()',1000);
} 
//-->
</SCRIPT>
</HEAD>
<BODY onLoad=&quot;startclock()&quot;> 
<div id=&quot;myclock&quot;></div>
</BODY>
</HTML>
This will only work with IE4+ and Nav6+. I haven't tested Mozilla or Opera yet. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
wouldn't it be safer cross browser wise just to change to a .write from the .status
document.write(monN+&quot; &quot;+nday+&quot;, &quot;+dayN+&quot; &quot;+nyear+&quot; &quot;+nhours+&quot;:&quot;+nmins+&quot; &quot;+AorP);

provide tools to let people become their best.
 
Thanks you guys.

Mission accomplished! :)
colorado.gif
 
Now I have a question.
I had used this script. But I am confused to what the differences are. I don't know an awful lot about the javascript, so is there something that I should look for that would tell me if it will run better or not?

<script
language=&quot;JavaScript&quot;>
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var timeValue = &quot;&quot; + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? &quot;:0&quot; : &quot;:&quot;) + minutes
timeValue += (hours >= 12) ? &quot; P.M.&quot; : &quot; A.M.&quot;
timerRunning = true;
mydate = new Date();
myday = mydate.getDay();
mymonth = mydate.getMonth();
myweekday= mydate.getDate();
weekday= myweekday;
myyear= mydate.getYear();
year = myyear
if(myday == 0)
day = &quot; Sunday, &quot;
else if(myday == 1)
day = &quot; Monday, &quot;
else if(myday == 2)
day = &quot; Tuesday, &quot;
else if(myday == 3)
day = &quot; Wednesday, &quot;
else if(myday == 4)
day = &quot; Thursday, &quot;
else if(myday == 5)
day = &quot; Friday, &quot;
else if(myday == 6)
day = &quot; Saturday, &quot;
if(mymonth == 0)
month = &quot;January &quot;
else if(mymonth ==1)
month = &quot;February &quot;
else if(mymonth ==2)
month = &quot;March &quot;
else if(mymonth ==3)
month = &quot;April &quot;
else if(mymonth ==4)
month = &quot;May &quot;
else if(mymonth ==5)
month = &quot;June &quot;
else if(mymonth ==6)
month = &quot;July &quot;
else if(mymonth ==7)
month = &quot;August &quot;
else if(mymonth ==8)
month = &quot;September &quot;
else if(mymonth ==9)
month = &quot;October &quot;
else if(mymonth ==10)
month = &quot;November &quot;
else if(mymonth ==11)
month = &quot;December&quot;
document.write(&quot;<B><P ALIGN=RIGHT><FONT SIZE=-2 FACE=VERDANA,ARIAL>&quot; + timeValue + &quot; | &quot; +day + month);
document.write(myweekday+&quot;,&quot;+ &quot; &quot; + year + &quot;</FONT></B><BR><HR WIDTH=210 ALIGN=RIGHT><P>&quot;);
</script>
 
First things first, the longer the if statement the slower we get. using arrays can cut your time in half in most cases. The browser has to read everyone of those else if's before it can determine the names and it may not seem like much now but when you get a page with two to three pages of script doing functions you can see a downfall in time loading and processing.

example is if it's December. You will not get a response until the statement reaches the last portion of the if statement. In reality we take sequential and make it random with arrays
I may not get it the 1st or 2nd time,
but how sweet that 15th time can be.
 
Ahh, I see. Back to the flat file deal.
Cool, thanks for the clarification.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top