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

2018-06-24 01:47:22来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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
    }
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:React简明学习

下一篇:正则表达式小记