• 转换时间格式 时区转换


    // 中国 东8区   韩国 东9区  印度 东5.5区
    export const getNewDate = function({ date, zone }) { // date 日期格式  zone 时区
      var timezone = zone || 8; //目标时区时间
      var dates = date || new Date()
      var offset_GMT = dates.getTimezoneOffset(); // Date中时间和格林威治的时间差,单位为分钟
      var nowDate = dates.getTime(); // Date中时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数
      var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);
      // console.log(`东${timezone}区现在是:` + targetDate)
      return targetDate
    }
    
    export const toTimestamp = function(time, zone) { // time 可以为时间戳,可为日期
      if (!time) return null
      if (arguments.length === 0) {
        return null
      }
      let date
      if (typeof time === 'object') {
        date = time
      } else {
        if ((typeof time === 'string')) {
          if ((/^[0-9]+$/.test(time))) {
            // support "1548221490638"
            time = parseInt(time)
          } else {
            // support safari
            // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
            time = time.replace(new RegExp(/-/gm), '/')
          }
        }
    
        if ((typeof time === 'number') && (time.toString().length === 10)) {
          time = time * 1000
        }
        date = new Date(time)
      }
      date = getNewDate({ date, zone })
      return date.getTime()
    }
    
    export function parseTime(time, cFormat, zone) { // time 可以为时间戳,可为日期
      if (!time) return null
      if (arguments.length === 0) {
        return null
      }
      const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
      let date
      if (typeof time === 'object') {
        date = time
      } else {
        if ((typeof time === 'string')) {
          if ((/^[0-9]+$/.test(time))) {
            // support "1548221490638"
            time = parseInt(time)
          } else {
            // support safari
            // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
            time = time.replace(new RegExp(/-/gm), '/')
          }
        }
    
        if ((typeof time === 'number') && (time.toString().length === 10)) {
          time = time * 1000
        }
        date = new Date(time)
      }
      date = getNewDate({ date, zone })
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
      }
      const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
        const value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
        return value.toString().padStart(2, '0')
      })
      return time_str
    }
    
    /**
     * @param {number} time
     * @param {string} option
     * @returns {string}
     */
    export function formatTime(time, option) { // time 时间戳 option
      if (('' + time).length === 10) {
        time = parseInt(time) * 1000
      } else {
        time = +time
      }
      const d = new Date(time)
      const now = Date.now()
    
      const diff = (now - d) / 1000
    
      if (diff < 30) {
        return '刚刚'
      } else if (diff < 3600) {
        // less 1 hour
        return Math.ceil(diff / 60) + '分钟前'
      } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前'
      } else if (diff < 3600 * 24 * 2) {
        return '1天前'
      }
      if (option) {
        return parseTime(time, option)
      } else {
        return (
          d.getMonth() +
          1 +
          '月' +
          d.getDate() +
          '日' +
          d.getHours() +
          '时' +
          d.getMinutes() +
          '分'
        )
      }
    }
    
    
  • 相关阅读:
    可变性编程 不可变性编程 可变性变量 不可变性变量 并发编程 命令式编程 函数式编程
    hashable
    优先采用面向表达式编程
    内存转储文件 Memory.dmp
    windows update 文件 路径
    tmp
    查询局域网内全部电脑IP和mac地址等信息
    iptraf 网卡 ip 端口 监控 netstat 关闭端口方法
    Error 99 connecting to 192.168.3.212:6379. Cannot assign requested address
    t
  • 原文地址:https://www.cnblogs.com/mary-123/p/13785891.html
Copyright © 2020-2023  润新知