JS Clock + Date (Real-Time)


/ Published in: JavaScript
Save to your folder(s)

Creates a DATE and TIME object that can be places on a site.
OPEN-SOURCE: Free to download, use, modify and distribute - so long as all Credits/Comments are left INTACT.
If you have any thoughts, comments or ideas for this script (or another script you'd like me to have a go at making) then please let me know!!
[email protected]


Copy this code and paste it in your HTML
  1. Put the following inside your <head> tags of the page you want the clock+date box to appear on.
  2. <script type="text/javascript">
  3. <!--
  4. // --------- --------- ---------
  5. // Javascript Real-time Clock+Date
  6. // Created By Darksider
  7. // http://www.swedger.com
  8. // --------- --------- ---------
  9. // This script is OPEN-SOURCE,
  10. // and as such can be downloaded,
  11. // modified, and re-distributed
  12. // by ANYONE. Though if you do,
  13. // PLEASE LEAVE THIS AND ALL OTHER
  14. // COMMENT BLOCKS INTACT!!
  15. // --------- --------- ---------
  16. function freshtime() {
  17. var timestamp=new Date();
  18. // - - - FIRST SORT THE TIME - - - \\
  19. var hour=timestamp.getHours();// hours passed today(0-23)
  20. var mins=timestamp.getMinutes();// minutes past the hour(0-59)
  21. var secs=timestamp.getSeconds();// seconds past the minute(0-59)
  22. var period; // variable to contain "am/pm" string
  23. if(hour==0) {hour=12;}// turns 0 into 1 (0 is returned for 12 midday and 12 midnight)
  24. if(hour>12) {hour=(hour-12);period="pm";} else {period="am";}// if hour is more than 12, deduct 12 and make the period PM - otherwise make period AM
  25. if(mins<10) {mins="0"+mins;}// add a trailing zero the the minutes variable if the minutes are less than 10
  26. if(secs<10) {secs="0"+secs;}// also add a trailing zero to the seconds variable if seconds < 10
  27. var curTime=hour+":"+mins+":"+secs+" "+period;// build out curTime string with the 4 blocks(hours:minutes:seconds <am/pm>)
  28. // - - - NEXT SORT THE DATE - - - \\
  29. var date=timestamp.getDate();// current date (0-30/31 or 28/29 for February[1])
  30. var month=timestamp.getMonth();// current month (0-11)
  31. var year=timestamp.getFullYear();// full year (4-digits)
  32. date=(date+1); // add 1 to the date, because they start at 0 (1st day)
  33. month=(month+1);// also add 1 because 0 = January
  34. if(date==1||date==21||date==31) {date=date+"st";}else if(date==2||date==22) {date=date+"nd";}else if(date==3||date==23) {date=date+"rd";}else {date=date+"th";}
  35. // - - - MAKE MONTH TEXTUAL - - - \\
  36. if(month==1) {month="January";}else if(month==2) {month="February";}else if(month==3) {month="March";}else if(month==4) {month="April";}
  37. else if(month==5) {month="May";}else if(month==6) {month="June";}else if(month==7) {month="July";}else if(month==8) {month="August";}
  38. else if(month==9) {month="September";}else if(month==10) {month="October";}else if(month==11) {month="November";}else if(month==12) {month="December";}
  39. var curDate=date+" "+month+" "+year;// build out curDate string with the 3 blocks(date/month/year)
  40. // - - - THEN PRINT DATE+TIME - - - \\
  41. document.getElementById("clock").firstChild.nodeValue=curDate+" "+curTime;
  42. }
  43. //-->
  44. </script>
  45.  
  46. that takes care of the hard part, now you just need to tell your page to continually update your clock(we will add the actual clock after this bit). either replace your first <body> tag with the following, or add the onLoad="" part into your current one:
  47. <body onLoad="freshtime();setInterval('freshtime()',999);">
  48.  
  49. the script above tells your page to initialize the time when your page is loaded, and refresh the clock with the current time (and date) every 999 milliseconds (.001 ms off of a second - just because i can!).
  50. So all you need now is the clock object for the script to update.
  51. I have already created a nice little rectangle box to contain it using CSS.
  52. For this example though, I will just use a style="" tag.
  53. Copy the following code into wherever you want the clock+date box to appear.
  54. (personally, I use CSS position:absolute; to position my stuff wherever I want - gives you more control. but you can just put this at the top/bottom of your page or wherever)
  55. <span id="clock" style="border:3px dotted;border-color:#000000;color:#000000;font-size:17;text-align:center;font-weight:bold;">&nbsp;</span>
  56.  
  57. That's it!
  58. You should now have a lovely little real-time box that updates (almost) every second.
  59. It adds an "am/pm" suffix to times dependent on the period (morning/afternoon) and also changes the month to a TEXTUAL format.
  60. The style stuff in there is to give it a lovely little dotted border.
  61.  
  62. Feel free to modify any part of this script(other than the comments), and redistribute it as you like, but please remember who made it. Please leave my credit in there so that future versions/users can see where the original effor came from - but feel free to even add in your own comment line(s) underneath to say that it was you who added in whatever it is you decide to add in/change.
  63.  
  64. Also- feel free to email me ( darksider(at)swedger(dot)com ) to let me know what you think of my script!!
  65.  
  66. -darksider-

URL: http://www.swedger.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.