Skip to content

Instantly share code, notes, and snippets.

@starfeeling
Created April 24, 2017 06:29
Show Gist options
  • Save starfeeling/a31c4388f13199ee2de3131770165a29 to your computer and use it in GitHub Desktop.
Save starfeeling/a31c4388f13199ee2de3131770165a29 to your computer and use it in GitHub Desktop.
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
function getDateDiff(dateTimeStamp) {
var now = new Date().getTime();
var diffValue = now - dateTimeStamp;
if (diffValue < 0) {
return false;
}
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (monthC >= 1) {
result = "post at" + parseInt(monthC) + "month ago";
} else if (weekC >= 1) {
result = "post at" + parseInt(weekC) + "week ago";
} else if (dayC >= 1) {
result = "post at" + parseInt(dayC) + "day ago";
} else if (hourC >= 1) {
result = "post at" + parseInt(hourC) + "hour ago";
} else if (minC >= 1) {
result = "post at" + parseInt(minC) + "minute ago";
} else
result = "just now";
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment