jsは時間をちょうど、数分前、数時間前、数日前、数ヶ月前に変えます.

1128 ワード

function getTimer(stringTime) {
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var week = day * 7;
    var month = day * 30;
    var time1 = new Date().getTime();//      
    console.log(time1);
    var time2 = Date.parse(new Date(stringTime));//        
    console.log(time2);
    var time = time1 - time2;

    var result = null;
    if (time < 0) {
        alert("             !");
    } else if (time / month >= 1) {
        result = "   :" + parseInt(time / month) + "  ";
    } else if (time / week >= 1) {
        result = "   :" + parseInt(time / week) + "  ";
    } else if (time / day >= 1) {
        result = "   :" + parseInt(time / day) + "  ";
    } else if (time / hour >= 1) {
        result = "   :" + parseInt(time / hour) + "   ";
    } else if (time / minute >= 1) {
        result = "   :" + parseInt(time / minute) + "   ";
    } else {
        result = "    !";
    }
    return result;
}
getTimer("2020-04-20 15:06:36")