Skip to content

Instantly share code, notes, and snippets.

@mikepenzin
Last active March 18, 2017 20:58
Show Gist options
  • Save mikepenzin/5ac3d38c14803a64cc1a848decafdb3e to your computer and use it in GitHub Desktop.
Save mikepenzin/5ac3d38c14803a64cc1a848decafdb3e to your computer and use it in GitHub Desktop.
Easy calculate how much time passed since update/creation
var createdTime ="Sat Dec 12 2016 18:04:54 GMT+0000";
(function DateTimeDifference( previousTime ) {
var today = Date.now();
previousTime = Date.parse(previousTime);
var seconds = (today - previousTime) / 1000;
var hours = parseInt( seconds / 3600 );
seconds = seconds % 3600;
var minutes = parseInt( seconds / 60 );
if(minutes < 60 && hours === 0) {
return(minutes === 1 ? Math.round(minutes) + " minute ago" : Math.round(minutes) + " minutes ago");
}
if(hours > 24){
var days = hours / 24;
if (days > 30) {
var month = days / 30;
if (month > 12) {
var years = month / 12;
if (years > 0) {
return(years === 1 ? Math.ceil(years) + " year ago" : Math.ceil(years) + " years ago");
}
}
return(Math.round(month) + " month ago");
}
return(days === 1 ? Math.round(days) + " day ago" : Math.round(days) + " days ago");
}
return(hours === 1 ? Math.round(hours) + " hour ago" : Math.round(hours) + " hours ago");
})(createdTime);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment