Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created August 4, 2013 17:11
Show Gist options
  • Save james2doyle/6151040 to your computer and use it in GitHub Desktop.
Save james2doyle/6151040 to your computer and use it in GitHub Desktop.
parse the twitter date(created_at) into a nice format.
function parseTwitterDate(tdate) {
var systemDate = new Date(Date.parse(tdate));
var userDate = new Date();
var diff = Math.floor((userDate - systemDate) / 1000);
if (diff <= 1) {return 'just now';}
if (diff < 20) {return diff + ' seconds ago';}
if (diff < 40) {return 'half a minute ago';}
if (diff < 60) {return 'less than a minute ago';}
if (diff <= 90) {return 'one minute ago';}
if (diff <= 3540) {return Math.round(diff / 60) + ' minutes ago';}
if (diff <= 5400) {return '1 hour ago';}
if (diff <= 86400) {return Math.round(diff / 3600) + ' hours ago';}
if (diff <= 129600) {return '1 day ago';}
if (diff < 604800) {return Math.round(diff / 86400) + ' days ago';}
if (diff <= 777600) {return '1 week ago';}
return 'on ' + systemDate;
}
Copy link

ghost commented Mar 22, 2019

thanks

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