时间函数优化

83 阅读2分钟

秒数转换为天,小时,分钟,秒的公式

秒数转换公式
int time;

int second=time%60;

int minute=time%3600/60;

int hour=time%(24x3600)/3600;

int day=time/(24x3600);

基础版本

// 根据秒数转换成对应的时分秒
export function getHMS(time) {
  const hour = parseInt(time / 3600) < 10 ? '0' + parseInt(time / 3600) : parseInt(time / 3600)
  const min = parseInt(time % 3600 / 60) < 10 ? '0' + parseInt(time % 3600 / 60) : parseInt(time % 3600 / 60)
  const sec = parseInt(time % 3600 % 60) < 10 ? '0' + parseInt(time % 3600 % 60) : parseInt(time % 3600 % 60)
  return hour + ':' + min + ':' + sec
}

js将秒转化为天时分秒 有0的去掉


export const setSeconds = (sencond) => {
  let result = ''
  //3600秒等于60分钟等于1小时
  if (sencond > 3600 * 24) {
    //表示大于一天
    let day = parseInt(sencond / (3600 * 24))
    // sencond = sencond % (3600 * 24) //求天数整除外剩余的秒数
    // let hour = parseInt(sencond / 3600) //1小时==60分钟==3600秒,所以剩余的秒数÷3600= 得到几个小时数
    let hour = parseInt((sencond / 3600) % 24)
    let min = parseInt((sencond / 60) % 60)
    let sec = parseInt(sencond % 60)
    if (sec > 0) {
      result = day + '天' + hour + '时' + min + '分' + sec + '秒'
    } else {
      if (min > 0) {
        result = day + '天' + hour + '时' + min + '分'
      } else {
        if (hour > 0) {
          result = day + '天' + hour + '时'
        } else {
          result = day + '天'
        }
      }
    }
  } else if (sencond > 3600) {
    //表示大于一个小时
    let hour = parseInt(sencond / 3600)
    let min = parseInt((sencond % 3600) / 60) //求小时数整除外剩余的秒数, 秒数÷60秒 = 得到几分钟
    let sec = parseInt(sencond % 60)
    if (sec > 0) {
      result = hour + '时' + min + '分' + sec + '秒'
    } else {
      if (min > 0) {
        result = hour + '时' + min + '分'
      } else {
        result = hour + '时'
      }
    }
  } else if (sencond > 60) {
    //表示大于一分钟
    let min = parseInt(sencond / 60)
    let sec = parseInt(sencond % 60) //求分钟数整除外剩余的秒数
    if (sec > 0) {
      result = min + '分' + sec + '秒'
    } else {
      result = min + '分'
    }
  } else {
    //不大于一天、不大于一个小时、也不大于一分钟,那就是秒数
    result = '' + parseInt(sencond) + '秒'
  }
  return result
}


   function setSeconds(sencond) {
        let result = ''
        let day =
          parseInt(sencond / (3600 * 24)) > 0
            ? parseInt(sencond / (3600 * 24)) + '天'
            : ''
        let hour =
          parseInt((sencond / 3600) % 24) > 0
            ? parseInt((sencond / 3600) % 24) + '时'
            : ''
        let min =
          parseInt((sencond / 60) % 60) > 0
            ? parseInt((sencond / 60) % 60) + '分'
            : ''
        let sec =
          parseInt(sencond % 60) > 0 ? parseInt(sencond % 60) + '秒' : ''
       if (sencond !== 0) {
    result = day + hour + min + sec
  } else {
    result = sencond + '秒'
  }
        return result
      }

      console.log('setSeconds', setSeconds(604800))
      console.log('setSeconds', setSeconds(604801))
      console.log('setSeconds', setSeconds(604860))
      console.log('setSeconds', setSeconds(604861))
      console.log('setSeconds', setSeconds(3602))
      console.log('setSeconds', setSeconds(2))

image.png

补零的

 formatSeconds(value) {
        let result = parseInt(value)
        let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
        let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
        let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));

        let res = '';
        if(h !== '00') res += `${h}h`;
        if(m !== '00') res += `${m}min`;
        res += `${s}s`;
        return res;
      }

字符串也可以拼接的

视频展示时分

www.cnblogs.com/codedisco/p…


      function secondToTimeStr(t) {
        if (!t) return
        if (t < 60) return '00:' + ((i = t) < 10 ? '0' + i : i)
        if (t < 3600)
          return (
            '' +
            ((a = parseInt(t / 60)) < 10 ? '0' + a : a) +
            ':' +
            ((i = t % 60) < 10 ? '0' + i : i)
          )
        if (3600 <= t) {
          var a,
            i,
            e = parseInt(t / 3600)
          return (
            (e < 10 ? '0' + e : e) +
            ':' +
            ((a = parseInt((t % 3600) / 60)) < 10 ? '0' + a : a) +
            ':' +
            ((i = t % 60) < 10 ? '0' + i : i)
          )
        }
      }

vue中将秒数转化为天时分秒,并倒计时

www.cnblogs.com/haoning/p/1…

export default {
  data () {
    return {
      countTime: 129600,
      _interval: ''toLiveBtn: '计时点击事件'
    }
  },
  methods: {
     // 倒计时事件
      countdown() {
        const that = this
        that._interval = setInterval(() => {
          if(that.countTime == 0) {
              // 计时结束,清除缓存
              clearInterval(that._interval)
            } else {
              that.countTime--
              let day = parseInt(that.countTime / 60 / 60 / 24)
              let hr = parseInt(that.countTime / 60 / 60 % 24)
              let min = parseInt(that.countTime / 60 % 60)
              let sec = parseInt(that.countTime % 60)

              day = day > 9 ? day : '0' + day
              hr = hr > 9 ? hr : '0' + hr
              min = min > 9 ? min : '0' + min
              sec = sec > 9 ? sec : '0' + sec
              that.toLiveBtn = `${day}${hr}${min}${sec}秒`
            }
        }, 1000);
      }
  },
  //当离开页面时,清除倒计时
  beforeDestroy() {
    clearInterval(this._interval)
  }
}

xx小时前

image.png

/** * 人性化时间 * @param {Object} timestamp */ function beautifyTime(timestamp){ var mistiming = Math.round(new Date() / 1000)-timestamp; var postfix = mistiming>0 ? '前' : '后' mistiming = Math.abs(mistiming) var arrr = ['年','个月','星期','天','小时','分钟','秒']; var arrn = [31536000,2592000,604800,86400,3600,60,1]; for(var i=0; i<7; i++){ var inm = Math.floor(mistiming/arrn[i]) if(inm!=0){ return inm+arrr[i] + postfix } } }

cloud.tencent.com/developer/a…

可输入指定函数

  function getSFM(seconds, dateFormat = 'H:i:s') {
        var obj = {}
        obj.H = Number.parseInt(seconds / 3600)
        obj.i = Number.parseInt((seconds - obj.H * 3600) / 60)
        obj.s = Number.parseInt(seconds - obj.H * 3600 - obj.i * 60)
        if (obj.H < 10) {
          obj.H = '0' + obj.H
        }
        if (obj.i < 10) {
          obj.i = '0' + obj.i
        }
        if (obj.s < 10) {
          obj.s = '0' + obj.s
        }

        // 3.解析
        var rs = dateFormat
          .replace('H', obj.H)
          .replace('i', obj.i)
          .replace('s', obj.s)
        return rs
      }
      
      
      `getSFM(100); ``// => 00:01:40`

`getSFM(100,``'H:i:s'``); ``// => 00:01:40`

`getSFM(100,``'H小时i分s秒'``); ``// => 00小时01分40秒`

www.cnblogs.com/polk6/p/151…