Convert to current time interval based on input timestamp

//Commonly used in forums, for example, after posting a comment, display just now

//The rules are as follows (can be changed according to the actual situation)

1: Return to """Just now" within ten minutes

2: within today --- display how many hours ago

3: within yesterday ---- yesterday hour: minute

4: Consistently displayed as 16-11-11 11:11

 

// Time conversion function, pass in timestamp
//pass in timestamp
function getDateDiff(dateTimeStamp) {
    var currentTime = {};
var result = "";

var now = new Date(); //当前时间
currentTime.currentYear = now.getFullYear();
currentTime.currentMonth = now.getMonth() + 1;
currentTime.currentDay = now.getDay();
currentTime.currentHour = now.getHours();
currentTime.currentMinute = now.getMinutes();
var thisTime = new Date(dateTimeStamp);

var thisNow = {};
thisNow.year = thisTime.getFullYear();
thisNow.month = thisTime.getMonth() + 1;
thisNow.day = thisTime.getDay();
thisNow.hour = (thisTime.getHours() < 10 ? ("0" + thisTime.getHours()) : thisTime.getHours());
thisNow.minute = (thisTime.getMinutes() < 10 ? ("0" + thisTime.getMinutes()) : thisTime.getMinutes());
if (now.getTime() - thisTime.getTime() < 0) {
        return;
}
    var isSameYear = (currentTime.currentYear == thisNow.year) ? true : false;
var isSameMonth = (currentTime.currentMonth == thisNow.month) ? true : false;
var isSameDay = (currentTime.currentDay == thisNow.
day) ? true : false;
var isSameHour = (currentTime.currentHour == thisNow.hour) ? true : false;
if (isSameYear && isSameMonth && isSameDay) {

        if (isSameHour) {
            //同年同月同时
var minuteSum = currentTime.currentMinute - thisNow.minute;
if (minuteSum <= 10) {
                result = "刚刚"
} else if (minuteSum < 60) {
                result = minuteSum + " minutes ago " ;
 }
        } else {
            result = (currentTime.currentHour - thisNow.hour) + "小时前";
}
    } else if (isSameYear && isSameMonth && !isSameDay) {
        //同年同月不同天
var daySum = currentTime.currentDay - thisNow.day;
if (daySum <= 1) {
            result = "昨天" + thisNow.hour + ":" + thisNow.minute;
} else {
            result = "16-11-11 11:11";
}
    } else {
        result = "16-11-11 11:11";
}
    return result;
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327073879&siteId=291194637