码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-06 18:55:27      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:必须   引入   ==   ===   获取   timer   floor   const   font   

      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秒的封装(简易版)

标签:必须   引入   ==   ===   获取   timer   floor   const   font   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!