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>
  • 相关阅读:
    数据提交
    Python网页信息抓取
    Python语法学习
    Elasticsearch5.x 升级-插件
    LeetCode 33 搜索旋转排序数组
    按之字形顺序打印二叉树
    股票的最大利润
    LeetCode 1143 最长公共子序列
    对称的二叉树
    两个链表的第一个公共结点
  • 原文地址:https://www.cnblogs.com/bao2333/p/10153865.html
Copyright © 2011-2022 走看看