時間格式化返回不一樣的時間描述

function getFormatTime(time){
    var current = (new Date()).getTime();
    var cross = current - time;
    cross = Math.round(cross/1000);
    if(cross < 2){
        return "剛剛發佈";
    }
    if(cross < 60){
        return cross+"秒前"
    }
    if(cross > 60 && cross < 3600){
        cross = Math.round(cross/60);
        return cross+"分鐘前"
    }
    if(cross > 3600 && cross < 3600*24){
        cross = Math.floor(cross/3600);
        return cross+"小時前"
    }
    if(cross > 3600*24){
        cross = Math.floor(cross/(3600*24));
        return cross+"天前"
    }
}
相關文章
相關標籤/搜索