js目标时间与当前时间差转换年月日天数

后端同同不肯给我算好的时间差给我,只好自己写了:

 getdate(4292488)  使用方法传入目标时间与当前时间差(时间戳)

function getdate(period) {
        var yearLevelValue = 365 * 24 * 60 * 60 * 1000;
        var monthLevelValue = 30 * 24 * 60 * 60 * 1000;
        var dayLevelValue = 24 * 60 * 60 * 1000;
        var hourLevelValue = 60 * 60 * 1000;
        var minuteLevelValue = 60 * 1000;
        var secondLevelValue = 1000;

        function getDifference(period) {
            /*******计算出时间差中的年、月、日、天、时、分、秒*******/
            var year = parseInt(getYear(period));
            var month = parseInt(getMonth(period - year * yearLevelValue));
            var day = parseInt(getDay(period - year * yearLevelValue - month * monthLevelValue));
            var hour = parseInt(getHour(period - year * yearLevelValue - month * monthLevelValue - day * dayLevelValue));
            var minute = parseInt(getMinute(period - year * yearLevelValue - month * monthLevelValue - day * dayLevelValue - hour * hourLevelValue));
            var second = parseInt(getSecond(period - year * yearLevelValue - month * monthLevelValue - day * dayLevelValue - hour * hourLevelValue - minute * minuteLevelValue));
            var result = "";
            if (year != 0)result = result + year + "年";
            if (month != 0)result = result + month + "月";
            if (day != 0)result = result + day + "天";
            result = result + hour + "时" + minute + "分" + second + "秒";
            function getYear(period) {
                return parseInt(period) / yearLevelValue;
            }
            function getMonth(period) {
                return parseInt(period) / monthLevelValue;
            }
            function getDay(period) {
                return parseInt(period) / dayLevelValue;
            }
            function getHour(period) {
                return parseInt(period) / hourLevelValue;
            }
            function getMinute(period) {
                return parseInt(period) / minuteLevelValue;
            }
            function getSecond(period) {
                return parseInt(period) / secondLevelValue;
            }
            return result;
        }
        return getDifference(period);
    }

你可能感兴趣的:(web前端)