zoukankan      html  css  js  c++  java
  • 时间格式化

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
      <!-- "2018-12-18T01:56:23.200Z" -->
    
    
      <script>
        /* 
          条件 ? true : 条件 ? true : 条件 ? true : false
                 1秒  1
          time < 60 // 1分钟 60
          time < 60 * 60 // 1小时 3600
          time < 60 * 60 * 24 // 1天  86400
          time < 60 * 60 * 24 * 7 // 1周 604800
          time < 60 * 60 * 24 * 30 // 1月 2592000
          time < 60 * 60 * 24 * 365 // 1年  31536000
    
          59   59秒前 
          70   1分钟前
          3700 1小时前
          86300
    
          拿着一个事件从后往前除 一旦有大于 1 就结束
        */
    
        function formatTime (time) {
          const d = new Date(time) // 获取到当前的时间对象
          const t = Math.floor((Date.now() - d.getTime()) / 1000)
    
          const arr = ['', '', '', '', '小时', '分钟', '']
          const arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1]
    
          // 遍历数据,根据数组中的值进行计算
          for (let i = 0; i < arrn.length; i++) {
            const day = Math.floor(t / arrn[i])
            if (day != 0) {
              return day + arr[i] + ""
            }
          }
        }
    
        console.log(formatTime("2018-12-20T23:00:23.200Z"))
        
      </script>
    </body>
    </html>
  • 相关阅读:
    vim技巧2
    vim技巧1
    网站压力测试工具
    CentOS mysql安装
    破解root
    渐进式性能监测案例
    网络监测介绍
    I/O检测介绍
    虚拟内存介绍
    @Slf4j
  • 原文地址:https://www.cnblogs.com/bao2333/p/10153865.html
Copyright © 2011-2022 走看看