毫秒转换为xx天xx时xx分xx秒

以毫秒为时间单位,计算当前毫秒为 **天 **时 **分 **秒

1.显示**天 **时 **分 **秒

主要代码:

time = 170276567;

getDuration(time) {
     
      var days = time / 1000 / 60 / 60 / 24;
      var daysRound = Math.floor(days);
      var hours = time / 1000 / 60 / 60 - 24 * daysRound;
      var hoursRound = Math.floor(hours);
      var minutes = time / 1000 / 60 - 24 * 60 * daysRound - 60 * hoursRound;
      var minutesRound = Math.floor(minutes);
      var seconds = time / 1000 - 24 * 60 * 60 * daysRound - 60 * 60 * hoursRound - 60 * minutesRound;
      var secondsRound = Math.floor(seconds);
      var timeRound = "";
      if (daysRound == 0) {
        timeRound =  hoursRound + "时" + minutesRound + "分" + secondsRound + "秒";
      } else if (daysRound !== 0) {
        timeRound = daysRound + "天" + hoursRound + "时" + minutesRound + "分" + secondsRound +"秒";
      } 
     
      }
     
      return timeRound;
    },

结果:

2.显示**月**天 **时 **分 **秒

time=2921499440,

getDuration(time) {
      // console.log(time,'time')
      var month = time / 1000 / 60 / 60 / 24 / 30;//月
      var monthRound = Math.floor(month);//月
      var days = time / 1000 / 60 / 60 / 24 - 30 * monthRound;
      var daysRound = Math.floor(days);

      var hours = time / 1000 / 60 / 60  -24* 30 * monthRound- 24 * daysRound;
      var hoursRound = Math.floor(hours);
      var minutes = time / 1000 / 60 -24* 30 * 60* monthRound - 24 * 60 * daysRound - 60 * hoursRound;
      var minutesRound = Math.floor(minutes);
      var seconds = time / 1000  -24* 30 * 60*60* monthRound- 24 * 60 * 60 * daysRound - 60 * 60 * hoursRound - 60 * minutesRound;
      var secondsRound = Math.floor(seconds);
      var timeRound = "";
      if (daysRound == 0) {
        timeRound =  hoursRound + "时" + minutesRound + "分" + secondsRound + "秒";
      } else if ( monthRound == 0) {
        timeRound =daysRound + "天" + hoursRound + "时" + minutesRound + "分" + secondsRound +"秒";
      } else if (monthRound != 0){
        timeRound =monthRound+"月"+  daysRound + "天" + hoursRound + "时" + minutesRound + "分" + secondsRound +"秒";

      }
      console.log('monthRound',monthRound)
      return timeRound;

    },

结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值