It occurs to me that I may have introduced an error into my code while trying to make it look pretty to post. The following line may need to be split into 2 lines b/c I'm not sure how javascript (or java) handles chaining on "new" objects:
so: var tstamp = new Date(tweet_arr.results[i].created_at).toLocaleString(); may need to be turned into: var tstamp = new Date(tweet_arr.results[i].created_at); tstamp = tstamp.toLocaleString(); -Chad On Wed, Dec 24, 2008 at 12:12 PM, Chad Etzel <[email protected]> wrote: > Here's the code I have been using for such a task (written in javascript). > Consider it a Christmas present :) > > var getdatediff = function (now_ms, gmt) > { > var dt_ms = Date.parse(gmt); > > var delta = now_ms - dt_ms; > delta = Math.floor(delta / 1000); // turn into seconds > > if (delta > 86400) { > return '' + Math.floor(delta/86400) + ' days ago'; > } else if (delta > 3600) { > return '' + Math.floor(delta/3600) + ' hours ' + Math.floor(delta/60) + > ' minutes ago'; > } else if (delta > 60) { > return '' + Math.floor(delta/60) + ' minutes ' + (delta % 60) + ' > seconds ago'; > } else { // seconds > return '' + (delta % 60) + ' seconds ago'; > } > > } > > > var main_or_whatever = function() > { > > //it is assumed "tweet_arr" is an array with tweets returned from a .json > API call or something... > > var tstamp = new Date(tweet_arr.results[i].created_at).toLocaleString(); > var now = new Date(); > var now_ms = now.getTime(); > > var datediff = getdatediff(now_ms, tstamp); > > //print datediff in some fashion > document.write(datediff); > > } > > > -Chad > > > On Wed, Dec 24, 2008 at 6:08 AM, Matthew <[email protected]>wrote: > >> >> " All of the clients >> and the web post friendly things like about 2 hours ago. Can anyone >> provide a standard routine for converting the pure date element into >> these english strings?" >> >> take a look at the way Rails does it as well, >> >> http://github.com/rails/rails/tree/master/actionpack/lib/action_view/helpers/date_helper.rb >> >> It's quite a common thing, >> so perhaps you should look at the source for Struts, >> or some similar java framework, >> see if you can steal the code directly from there. >> > >
