export function now() { var d = new Date(); var year = d.getFullYear(); var month = change(d.getMonth() + 1); var day = change(d.getDate()); var hour = change(d.getHours()); var minute = change(d.getMinutes()); var second = change(d.getSeconds()); function change(t) { if (t < 10) { return '0' + t; } else { return t; } } return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; } export function timeFormat(usedTime = 0) { let hour = 0; let minute = 0; let second = 0; let sumTime = usedTime; if (sumTime) { if (sumTime >= 3600) { hour = Math.floor(sumTime / 3600) % 24; sumTime = (sumTime % 3600); } if (sumTime >= 60) { minute = Math.floor(sumTime / 60); sumTime = sumTime % 60; } second = Math.floor(sumTime); } function getTimeStr(val) { return val < 10 ? '0' + val : val; } return getTimeStr(hour) + ':' + getTimeStr(minute) + ':' + getTimeStr(second); } // 时分秒转为时间戳 export function toTimeStamp(time = '') { let s = 0; const hour = time.split(':')[0]; const min = time.split(':')[1]; const sec = time.split(':')[2]; s = Number(hour * 3600) + Number(min * 60) + Number(sec); return s; } // 时间戳的只转为时分秒 export function formatDuring(mss) { const now = new Date(mss); const hour = now.getHours(); // 返回日期中的小时数(0到23) const minute = now.getMinutes(); // 返回日期中的分钟数(0到59) const second = now.getSeconds(); // 返回日期中的秒数(0到59) return hour + ':' + minute + ':' + second; } export function prefixIntrger(num, length) { return (Array(length).join('0') + num).slice(-length); } /** 根据秒计算时间hh:mm:ss */ export function computationTime(time) { let hours = Math.floor(time / 3600); const newTime = time % 3600; let minutes = Math.floor(newTime / 60) + ''; let seconds = newTime % 60; if (hours < 0) { hours = '00'; } else if (hours < 10) { hours = '0' + hours; } if (minutes < 0) { minutes = '00'; } else if (minutes < 10) { minutes = '0' + minutes; } if (seconds < 0) { seconds = '00'; } else if (seconds < 10) { seconds = '0' + seconds; } return hours + ':' + minutes + ':' + seconds; } export function computationSeconds(seconds, hourUnit, minuteUnit, secondUnit) { if (seconds) { const h = parseInt(seconds / 3600); const m = parseInt((seconds - h * 3600) / 60); const s = (seconds - h * 3600) % 60; return h ? `${h} ${hourUnit} ${m} ${minuteUnit} ${s} ${secondUnit}` : m ? `${m} ${minuteUnit} ${s} ${secondUnit}` : `${s} ${secondUnit}`; } return null; }