Logo
技术 转载 C#

js实现时间显示几天前、几小时前或者几分钟前

photo
Pastore Antonio

2023年10月23日

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();//当前的时间戳
    var time2 = stringTime*1000;//指定时间的时间戳
    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;
}

本文为原创文章,请注意保留出处!
修复群晖Synology Drive client右键菜单缺失问题 Local, clean & environmental 作者:Pastore Antonio
1808 浏览量
1785 浏览量
configure: error: Package requirements (oniguruma) were not met Local, clean & environmental 作者:Pastore Antonio
1526 浏览量
Adobe Acrobat Pro 激活 Local, clean & environmental 作者:Pastore Antonio
1522 浏览量
追寻日出,找回自己 Local, clean & environmental 作者:Pastore Antonio
1489 浏览量