首页 > 其他 > 详细

时间转成x时x分x秒的封装(简易版)

时间:2019-12-06 18:41:59      阅读:99      评论:0      收藏:0      [点我收藏+]
      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(你获取到时间)就可以了 

注意:传入的时间必须为秒

时间转成x时x分x秒的封装(简易版)

原文:https://www.cnblogs.com/ayujun/p/11996813.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!