jsはどれぐらいの時間を発表しましたか?数分前、数時間前、数日前、数ヶ月前、数年前.





    
    Title


    function getTs(time){
    var arr = time.split(/[- :]/),
    _date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]),
    timeStr = Date.parse(_date)
    return timeStr
}    function handlePublishTimeDesc(post_modified){
        //                ,        
        var curTime = new Date();
        var postTime = new Date(post_modified);                  //                     (                       )
        
        //var timeDiff = curTime.getTime() - postTime.getTime();
        //            (      )
        var timeDiff = curTime.getTime() - getTs(post_modified);

        //     
        var min = 60 * 1000;
        var hour = min * 60;
        var day = hour * 24;
        var week = day * 7;
        var month =  week*4;
        var year = month*12;

        //               、 、 、 
        var  exceedyear = Math.floor(timeDiff/year);
        var exceedmonth = Math.floor(timeDiff/month);
        var exceedWeek = Math.floor(timeDiff/week);
        var exceedDay = Math.floor(timeDiff/day);
        var exceedHour = Math.floor(timeDiff/hour);
        var exceedMin = Math.floor(timeDiff/min);

        
        //                 ,  return

        if(exceedyear<100&&exceedyear>0){
            return exceedyear + '  ';
            }else{
            if(exceedmonth<12&&exceedmonth>0){
                return exceedmonth + '  ';
                }else{
                if(exceedWeek<4&&exceedWeek>0){
                    return exceedWeek + '   ';
                    }else{
                    if(exceedDay < 7 && exceedDay > 0){
                        return exceedDay + '  ';
                        }else {
                        if (exceedHour < 24 && exceedHour > 0) {
                            return exceedHour + '   ';
                        } else {
                            return exceedMin + '   ';
                        }
                    }
                    }
                }
            }
    }
    window.onload(alert(handlePublishTimeDesc("2018-10-26 15:33:0.0")));





少し修正して転載します.https://blog.csdn.net/Charles_Tian/articale/detail/81701556