时间戳转时间对象
-
传入时间戳间隔,返回时间对象,没有则为 0。(一般用作倒计时)
/** * 时间戳转时间对象 * @param {*} time 时间戳(毫秒) * @param {*} add0 不满10前面是否添加0 * @returns {Object} 时间对象 */ const timestampTrans = (time, add0 = true) => { let day = Math.floor(time / (1000 * 60 * 60 * 24)) let hours = Math.floor((time / (1000 * 60 * 60)) % 24) let minutes = Math.floor((time / (1000 * 60)) % 60) let seconds = Math.floor((time / 1000) % 60) const funAdd0 = (num) => num >= 10 ? num + '' : '0' + num if (add0) { day = funAdd0(day) hours = funAdd0(hours) minutes = funAdd0(minutes) seconds = funAdd0(seconds) } return { day, hours, minutes, seconds } } export default timestampTrans
-
时间戳转时间对象 (年月日时分秒)
/** * 时间戳转时间对象 * @param {*} time 时间戳(毫秒) * @param {*} add0 不满10前面是否添加0 * @returns {Object} 时间对象 */ const timestampTrans = (time, add0 = true) => { const date = new Date(time) const funAdd0 = (num) => num >= 10 ? num + '' : '0' + num let Y = date.getFullYear() let M = date.getMonth() + 1 let D = date.getDate() let h = date.getHours() let m = date.getMinutes() let s = date.getSeconds() if (add0) { Y = funAdd0(Y) M = funAdd0(M) D = funAdd0(D) h = funAdd0(h) m = funAdd0(m) s = funAdd0(s) } return { Y, M, D, h, m, s } }