Revolution X

Coding Community => Coding Snippets => Topic started by: Celebrus on April 30, 2009, 02:01:17 am



Title: Auto-Update the Time
Post by: Celebrus on April 30, 2009, 02:01:17 am
Preview: http://celebrusisawesome.smfforfree.com/

Has been tested on Firefox, Epiphany and Opera.

First ensure you have this in your headers:
Code:
<script src="/jquery.js" type="text/javascript"></script>

If you have that, proceed. If you don't, add it and return.

Then stick this in your footers:
Code:
<script type="text/javascript">
/**
 * Auto-Update the Time
 * http://aetus.net/
 * http://revolutionx.smfforfree3.com/
 *
 *  While there is NO warranty on this script unless you pay me a very large fee,  I will *TRY*
 *  to the best of my ability to help you out.
 *
 *  License: WTFPL (http://sam.zoy.org/wtfpl/)
 *  
 *  Feel free to remove this copyright notice, just visit those two sites listed above.
 *  Thanks for using this script.
 *
 */
function getTimeString() {
  var t = new Date();
  var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July',
  'August', 'September', 'October', 'November', 'December' ];
  var month = months[t.getMonth()];
  var date = t.getDate();
  var year = t.getFullYear();
  var hour = t.getHours();
  var suffix = "am";
  if (hour >= 12) {
    suffix = "pm";
    hour = (hour == 12) ? "12" : hour - 12;
  }
  if(hour < 10) { hour = "0" + hour; } 
  var minute = t.getMinutes();
  if(minute < 10) { minute = "0" + minute; }
  var second = t.getSeconds();
  if(second < 10) { second = "0" + second; }
  return month + " " + date + ", " + year + ", " + hour + ":" + minute + ":" + second + " " + suffix;
}
function updateTheDate($) {
  var timeString = getTimeString();
  var timeDisplay = $("div.tborder table tbody tr td.titlebg2 span.smalltext:contains(pm)");
  if(timeDisplay.length === 0) {
    timeDisplay = $("div.tborder table tbody tr td.titlebg2 span.smalltext:contains(am)");
  }
  timeDisplay.html(timeString);
}
setInterval("updateTheDate(jQuery)", 1000);
</script>

And voila, you now have a clock that shows the time *right now*, not when the page was loaded. And the time is from the user's computer, so you (and your members) don't have to mess with time offsets.


Title: Re: Auto-Update the Time
Post by: Agent Moose on April 30, 2009, 07:56:48 am
Nice code Cele.  Havn't seen a code from you that is javascript in forever :)


Title: Re: Auto-Update the Time
Post by: Exilis on April 30, 2009, 03:52:45 pm
Cool... But it's showing me 24 hour time, and it still says PM.


Title: Re: Auto-Update the Time
Post by: Celebrus on May 01, 2009, 12:08:23 am
Weird, this line is supposed to fix that:
    hour = (hour == 0) ? hour - 12 : "12";


Title: Re: Auto-Update the Time
Post by: Exilis on May 01, 2009, 06:09:11 am
That's weird- It wasn't twenty-hour time... It couldn't have been, because it was 4:50 for me. The clock said 12:50 o.O
Let me try the preview again...

EDIT: W00T I didn't even notice- I'm a hero member on RX- Well, I would be if the membergroups weren't changed on here...


Title: Re: Auto-Update the Time
Post by: Celebrus on May 01, 2009, 06:13:15 am
Ok there is that problem. I'm fixing it!


Title: Re: Auto-Update the Time
Post by: Exilis on May 01, 2009, 06:17:10 am
Weird. Works now. Thanks!


Title: Re: Auto-Update the Time
Post by: Celebrus on May 01, 2009, 06:20:00 am
Weird. Works now. Thanks!

That's because I just fixed it. I still have to see what happens when the hour is 12...


Title: Re: Auto-Update the Time
Post by: Blighted on May 20, 2009, 01:28:14 pm
great I've been fed up with having to put up with dodgy forum times thanks celebrus


Title: Re: Auto-Update the Time
Post by: Lyle on June 07, 2009, 04:03:19 pm
this is cool, but doesnt the time update by itself anyways/.?


Title: Re: Auto-Update the Time
Post by: Agent Moose on June 07, 2009, 05:04:08 pm
It does, but this one makes it in Real Time.  So it will change every second.