Skip to content

Instantly share code, notes, and snippets.

@mattpass
Created June 9, 2012 11:49
Show Gist options
  • Select an option

  • Save mattpass/2900665 to your computer and use it in GitHub Desktop.

Select an option

Save mattpass/2900665 to your computer and use it in GitHub Desktop.
Live server datetime golfing...
<span id="serverDT"></span>
<script>
var nDT = <?php echo time()*1000; ?>;
setInterval(function(){
var s=(new Date(nDT+=1e3)+'').split(' '),
d=s[2]*1,
t=s[4].split(':'),
p=t[0]>11?'pm':'am',
e=d%20==1|d>30?'st':d%20==2?'nd':d%20==3?'rd':'th';
t[0]=--t[0]%12+1;
document.getElementById('serverDT').innerHTML=[s[0],d+e,s[1],s[3],t.join(':')+p].join(' ');
},1000);
</script>
@maettig

maettig commented Jun 9, 2012

Copy link
Copy Markdown

There is still a lot you can do to make this shorter including dropping the PHP part (this will display the client time instead of the server time).

<span id="serverDT"></span>
<script>
var nDT = <?php echo time() * 1000; ?>;
setInterval(function(){
  var s=(new Date(nDT+=1e3)+'').split(' '),
    d=s[2]*1,
    t=s[4].split(':'),
    p=t[0]>11?'pm':'am',
    e=d%20==1|d>30?'st':d%20==2?'nd':d%20==3?'rd':'th';
  t[0]=--t[0]%12+1;
  document.getElementById('serverDT').innerHTML=[s[0],d+e,s[1],s[3],t.join(':')+p].join(' ');
},1000);
</script>

Edit: Bugfix. Displays server time again. Saved 2 bytes.

@mattpass

mattpass commented Jun 9, 2012

Copy link
Copy Markdown
Author

@maettig Excellent. Now under the 500 mark at 489 chars (from your suggestion plus removing unncessary brackets on line 7 and whitespace on line 11).

@mattpass

mattpass commented Jun 9, 2012

Copy link
Copy Markdown
Author

468 chars now, but I've only really achieved that by removing whitespace. Apart from reducing var names, putting everything onto 1 line, removing indents or semicolons at line end I think this is it?

@mattpass

mattpass commented Jun 9, 2012

Copy link
Copy Markdown
Author

3 more chars saved by using dS=String(date).split(' ');
(instead of using toString)

@mattpass

mattpass commented Jun 9, 2012

Copy link
Copy Markdown
Author

@maettig It's the server DT I'm really looking to display but a lot of those changes could be used for that I believe?

@maettig

maettig commented Jun 9, 2012

Copy link
Copy Markdown

Sure. I did an update to my code above.

@mattpass

mattpass commented Jun 9, 2012

Copy link
Copy Markdown
Author

377 chars now thanks to @maettig. Some v good changes, only drawback I can see is getting rid of the function so you see nothing for the first 1 sec and am I right in thinking those vars leak to global?

(As I understand it.... var a=1,b=2 ...b leaks to global? Or is it nothing escapes from anon functions?)

@maettig

maettig commented Jun 9, 2012

Copy link
Copy Markdown

No, var a=1,b=2 does not leak. To fix the delay you can put something like <?php echo strftime("..."); ?> in the <span>. Edit: I saved another two bytes in the code above.

@mattpass

mattpass commented Jun 9, 2012

Copy link
Copy Markdown
Author

@maettig Mmmm OK, I'm sure someone told me doing that leaks? Oh well, learnt something as well then :)

Don't mind the delay (gives the appearance of actually linking in with the server) ;)

Am integrating this as a final solution into ICEcoder today. Many thanks!

@p01

p01 commented Jun 9, 2012

Copy link
Copy Markdown

I know you're aiming for a specific format but why not let the end user in control and use the locale Date format ?

<span id="serverDT"></span>
<script>
var nDT = <?php echo time() * 1000; ?>;
setInteval("document.getElementById('serverDT').textContent=new Date(nDT+=1e3).toLocaleString()",1000);
</script>

@mattpass

mattpass commented Jun 9, 2012

Copy link
Copy Markdown
Author

@p01 Good point. I chose that format because it looks a little nicer than the regular Date output. In the bigger scheme of things I don't mind the extra 100 bytes or so on my IDE to have this format.

@mattpass

Copy link
Copy Markdown
Author

@maettig Good spot, updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment