毫秒或者秒转换为日期格式

function timeToMillion(startStr, endStr) {
        var times;
      //如果是2个参数就是时间差
if (endStr) { var startT = new Date(startStr).getTime() var endT = new Date(endStr).getTime() times = (endT - startT) / 1000
      // 如果是一个参数,参数值是秒数
} else { times = startStr } var day, hour, minute,endOutStr; if (times > 0) { // console.log(times) day = Math.floor(times / (60 * 60 * 24)); hour = Math.floor(times / (60 * 60)) - (day * 24); minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60); // second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60); if (parseInt(day) != 0) { endOutStr = day + "天" + hour + "小時" + minute + "分鐘" } else { if (parseInt(hour) != 0) { endOutStr = hour + "小時" + minute + "分鐘" } else { endOutStr = minute + "分鐘" } } }else{ endOutStr = 0 } // if (day <= 9) day = '0' + day; // if (hour <= 9) hour = '0' + hour; // if (minute <= 9) minute = '0' + minute; // if (second <= 9) second = '0' + second; return endOutStr }

 

/*
* timeToRemaining(seconds)秒數,返回剩餘的大概時間,有天數直接返回天數,沒天數有小時直接返回小時數,沒有小時數有分鐘數直接返回分鐘數,
* 沒有分鐘數直接返回傳入的秒數
*
**/ 

function timeToRemaining(seconds) {
    var times = parseInt(seconds) || 0;
    var day, hour, minute, second, endOutStr;
    if (times && times > 0) {
        day = Math.floor(times / (60 * 60 * 24));
        hour = Math.floor(times / (60 * 60)) - (day * 24);
        minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
        // second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
        second = times

        if (parseInt(day) != 0) {
            return endOutStr = day + "天"
        } else {
            if (parseInt(hour) != 0) {
                return endOutStr = hour + "小時"
            } else {
                if (parseInt(minute) != 0) {
                    return endOutStr = minute + "分鐘"
                }else{
                    return endOutStr = second + "秒"
                }
            }
        }
    } else {
        return endOutStr = 0
    }
}

 

posted @ 2018-03-26 17:26  逗伴不是瓣  阅读(5668)  评论(2编辑  收藏  举报