将N秒转换成时分秒中文形式(计算时长)

const formatTime = seconds => {
    const h = Math.floor(seconds / 3600);
    const m = Math.floor((seconds % 3600) / 60);
    const s = seconds % 60;
    let result = '';
    if (h > 0) {
        result += `${h}:`;
    }
    result += `${m < 10 ? `0${m}` : m}:${s < 10 ? `0${s}` : s}`;
    return result;
};



formateTime = time => {
    const h = parseInt(time / 3600, 10);
    const minute = parseInt((time / 60) % 60, 10);
    const second = Math.ceil(time % 60);
    const hours = h < 10 ? `0${h}` : h;
    const formatSecond = second > 59 ? 59 : second;
    let nowTime = '0秒';
    if (h) {
        nowTime = `${hours > 0 ? `${hours}时` : '00时'}${minute < 10 ? `0${minute}` : minute}分${
            formatSecond < 10 ? `0${formatSecond}` : formatSecond
        }秒`;
    } else if (minute) {
        nowTime = `${minute < 10 ? `0${minute}` : minute}分${
            formatSecond < 10 ? `0${formatSecond}` : formatSecond
        }秒`;
    } else if (formatSecond) {
        nowTime = `${formatSecond < 10 ? `0${formatSecond}` : formatSecond}秒`;
    } else {
        nowTime = '0秒';
    }
    return nowTime;
};



// 计算时长 转化成天 时 分 秒
const computeDuration = duration => {
    const day = parseInt(duration / (24 * 60 * 60 * 1000), 10);
    const dayRemainder = duration % (24 * 60 * 60 * 1000);
    const hour = parseInt(dayRemainder / (60 * 60 * 1000), 10);
    const hourRemainder = dayRemainder % (60 * 60 * 1000);
    const minute = parseInt(hourRemainder / (60 * 1000), 10);
    const minuteRemainder = hourRemainder % (60 * 1000);
    const second = parseInt(minuteRemainder / 1000, 10);
    return ((day && `${day}天`) || '') + ((hour && `${hour}小时`) || '') + ((minute && `${minute}分钟`) || '') + ((second && `${second}秒`) || '');
};






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值