Jquery计算时间戳之间的差值,可返回年,月,日,小时等

复制代码
/**
 * 计算时间戳之间的差值
 * @param startTime 开始时间戳
 * @param endTime 结束时间戳
 * @param type 返回指定类型差值(year, month, day, hour, minute, second)
 */
function DateDiff(startTime, endTime, type) {
    var timeDiff = endTime - startTime
    switch (type) {
        case "year":
            return Math.floor(timeDiff / 86400 / 365);
            break;
        case "month":
            return Math.floor(timeDiff / 86400 / 30);
            break;
        case "day":
            return Math.floor(timeDiff / 86400);
            break;
        case "hour":
            return Math.floor(timeDiff / 3600);
            break;
        case "minute":
            return Math.floor(timeDiff / 60);
            break;
        case "second":
            return timeDiff % 60;
            break;
    }
复制代码

 

posted @   James·wang  阅读(743)  评论(1编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示