function createTime(t) { let timer; if (t <= 0 || !t || t < 60 || typeof(t)!==‘number‘) timer = "default"; if (t >= 3600) timer = "hours"; if (t < 3600 && t >= 60) timer = "minutnes"; const defaul = () => { return ""; }; const hours = () => { const hour = Math.floor(t / 3600); const remainder1 = t % 3600; if (remainder1 === 0) return hour + "小时"; else if (remainder1 < 60) { return hour + "小时" + remainder1 + "秒"; } else { const min = Math.floor(remainder1 / 60); const remainder2 = remainder1 % 60; return `${hour}小时${min}分${ remainder2 === 0 ? "" : remainder2 + "秒" }`; } }; const minutnes = () => { const min = Math.floor(t / 60); const remainder1 = t % 60; return `${min}分${remainder1 === 0 ? "" : remainder1 + "秒"}`; }; const map = new Map([ ["default", (() => defaul())()], ["hours", (() => hours())()], ["minutnes", (() => minutnes())()], ]); return map.get(timer); } 使用的时候直接引入 然后createTime(你获取到时间)就可以了
注意:传入的时间必须为秒
原文地址:https://www.cnblogs.com/ayujun/p/11996813.html
时间: 2024-10-08 22:07:51