DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

JavaScript Date & Time Display

This is a discussion on JavaScript Date & Time Display within the JavaScript Code Samples forums, part of the JavaScript Programming category; I found this script and it appears to work great in both Internet Explorer and Firefox. Code: // Clock Script ...

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming > JavaScript Code Samples

  #1  
Old August 13th, 2008, 08:46 PM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
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.

Comments on this post
Mythily agrees: Thanked Post
__________________
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.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
The Following 2 Users Say Thank You to jmurrayhead For This Useful Post:
Mythily (April 22nd, 2009), richyrich (August 14th, 2008)
Reply

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming > JavaScript 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


Similar Threads

Thread Thread Starter Forum Replies Last Post
Time Entry Form sbenj69 Access Database Samples 5 May 1st, 2009 05:48 AM
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
Display Assembly File Date And Time Wolffy .Net 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 02:41 AM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2