Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples

Sponsored Links

Discuss "JavaScript Date & Time Display" in the Code Samples forum.

Code Samples - Search through our code samples to give your application that something extra or provide a code sample of your own.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old August 13th, 2008, 08:46 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 817
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default JavaScript Date & Time Display

I found this script and it appears to work great in both Internet Explorer and Firefox.

Code:
// Clock Script Generated By Maxx Blade's Clock v2.0d
// http://www.maxxblade.co.uk/clock
function timeSource(){
   x=new Date();
   x.setTime(x.getTime());
   return x;
}
function leadingZero(x){
   return (x>9)?x:'0'+x;
}
function twelveHour(x){
   if(x==0){
      x=12;
   }
   return (x>12)?x-=12:x;
}
function displayTime(){
   document.getElementById('tP').innerHTML=eval(outputTime);
   setTimeout('displayTime()',1000);
}
function amPMsymbol(x){
   return (x>11)?'pm':'am';
}
function fixYear4(x){
   return (x<500)?x+1900:x;
}

var dayNames=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var monthNames=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var outputTime="dayNames[timeSource().getDay()]+', '+leadingZero(timeSource().getDate())+' '+monthNames[timeSource().getMonth()]+' '+fixYear4(timeSource().getYear())+' '+twelveHour(timeSource().getHours())+':'+leadingZero(timeSource().getMinutes())+':'+leadingZero(timeSource().getSeconds())+amPMsymbol(timeSource().getHours())";
I simple save the above in a .js file. Then, in your page, have the following code:

Code:
<span id="tP">
    <script language="javascript" type="text/javascript" src="clock.js">
         //<![CDATA[
        displayTime();
         //]]>
     </script>    
</span>
The original, generated script had an extra line that automatically loaded the script without the need of calling displayTime() in the <span></span> above. However, this generated JavaScript errors in IE7...so I eventually came up with the above solution.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
The Following User Says Thank You to jmurrayhead For This Useful Post:
richyrich (August 14th, 2008)
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
edit one at a time guddu JavaScript Programming 4 August 21st, 2008 10:57 AM
keep time and date javier_83 Microsoft Access 6 August 15th, 2008 06:03 PM
Time Entry Form sbenj69 Microsoft Access 2 July 14th, 2008 02:11 PM
[ASP.Net/C#] Display Assembly File Date And Time Wolffy Code Samples 0 April 9th, 2008 11:57 AM
Site Down Time jmurrayhead Announcements 0 March 30th, 2008 09:42 AM


All times are GMT -4. The time now is 01:04 PM.



Content Relevant URLs by vBSEO 3.2.0