2

What script SO uses for dates displaying? Because it seems to be pretty nice formatting and logical showing.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Doolkin
  • 119
  • 3
  • 8

2 Answers2

3

I am not sure what StackOverflow use. But one of the most common timestamp representaion is the one done by twitter which displays timestamp as a moment ago, 30 seconds ago, x minutes ago, yesterday, 10:30 PM Apr 12, 2010 etc. And it updates the timestamp every five seconds without hitting the server.

If you are interested you may look into John Resig's Pretty Date JS API. It's just awesome. Works with/without JQuery.

We tweaked it a bit to exactly match Twitter pattern. And it is awesome.


The website mentioned gives good example, but if you so want a working version, put the following script in address bar of any web-page. (you may want to tweak parameters passed to prettyDate function

  javascript:var i,s,ss=['http://ejohn.org/files/pretty.js'];for(i=0;i!=ss.length;i++){s=document.createElement('script');s.src=ss[i];document.body.appendChild(s);}alert("PrettyDate: "+(prettyDate("2011-03-13T03:24:17Z")?prettyDate("2011-03-13T03:24:17Z"):"03:24 AM Mar 13, 2011"));
Nishant
  • 54,584
  • 13
  • 112
  • 127
  • 1
    I like how it's limited to the past month and cannot do dates beyond that, or any future dates. (I prefer [timeago](http://timeago.yarp.com).) – Lightness Races in Orbit Mar 14 '11 at 07:59
  • @Tomalak it is not meant to work for future date. Because usually you map an event that _has_ passed. But, tweaking the code is dead simple. We tweaked it quite a bit. Also, we removed the feature of last month, becasue after 2 - 3 weeks people would want to see exact timestamp. But as I said, just open the JS code, it's simple to adopt to your use case. – Nishant Mar 14 '11 at 08:02
  • Would be better if it weren't so pointlessly limited out of the box. I don't want some developer telling me what my "usual" use case is... and getting it wrong. – Lightness Races in Orbit Mar 14 '11 at 08:03
  • @Tomalak yeah http://timeago.yarp.com/ is also good one for some reasons it did not fit into our needs at that time. – Nishant Mar 14 '11 at 08:06
  • 1
    @Doolkin no, after a month it starts returning undefined where you'll print exact timestamp. – Nishant Mar 14 '11 at 08:10
  • @Nishant - i'm trying to use it, but i'm really confused from this script and the demo...can you show me some simple example how i should use it? – Doolkin Mar 14 '11 at 09:11
  • @Doolkin did you try the javascript I have updated in my answer? – Nishant Mar 15 '11 at 13:16
1

I think it's based on the answers to this question on Stack Overflow asked by Jeff Atwood - most of the answers are in c# but there is a PHP implementation too

Community
  • 1
  • 1
Andrew
  • 12,991
  • 15
  • 55
  • 85