Skip to content

Instantly share code, notes, and snippets.

@japboy
Created February 1, 2012 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japboy/1714823 to your computer and use it in GitHub Desktop.
Save japboy/1714823 to your computer and use it in GitHub Desktop.
My stupid JavaScript snippets
/**
* Date related snippets
* @author Yu I.
*/
(function() {
var datetime_rfc1123 = new Date(),
datetime_iso8601 = 'YYYY-MM-DDThh:mm:ss+0000',
unix_timestamp_1 = (datetime_rfc1123).getTime() / 1000.0,
unix_timestamp_2 = Date(datetime_rfc1123).parse,
convertTimestamp = function(timestamp) {
return new Date(timestamp * 1000);
},
/**
* Timeago!
*/
getTimeago = function(timestamp) {
var MINUTE = 60.0,
HOUR = 3600.0,
DAY = 86400.0,
WEEK = 604800.0,
MONTH = 2419200.0,
YEAR = 29030400.0,
current = (new Date()).getTime() / 1000.0,
past = timestamp,
diff = current - past,
timeago = 'Less than a minute';
if (0 < Math.floor(diff / YEAR)) {
timeago = Math.floor(diff / YEAR) + 'year(s) ago';
}
else if (0 < Math.floor(diff / MONTH)) {
timeago = Math.floor(diff / MONTH) + 'month(s) ago';
}
else if (0 < Math.floor(diff / WEEK)) {
timeago = Math.floor(diff / WEEK) + 'week(s) ago';
}
else if (0 < Math.floor(diff / DAY)) {
timeago = Math.floor(diff / DAY) + 'day(s) ago';
}
else if (0 < Math.floor(diff / HOUR)) {
timeago = Math.floor(diff / HOUR) + 'hour(s) ago';
}
else if (0 < Math.floor(diff / MINUTE)) {
timeago = Math.floor(diff / MINUTE) + 'minute(s) ago';
}
return timeago;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment