微信小程序中将时间戳转换为聊天格式(一)

该代码是对第三方库内容进行转换以方便使用wxs,也可以作为js使用:
https://www.npmjs.com/package/chat-date-format
使用方式:

调用:
chatDateFormat(timestamp, isDetail)

参数说明:
timestamp - 传入时间戳
isDetail - 是否要显示详细时间

let chatDateFormat = require('chat-date-format')

    methods: {
      ...chatDateFormat
    }

    created () {
      this.chatDateFormat(new Date()) // 当前格式化时间 - 某天 xx:xx
      this.chatDateFormat(new Date(), true) // 出当前格式化详细时间 - 某天 上午 xx:xx
    }

代码如下:

  /**
   * 格式化为IM所需显示时间
   * @param {Date} timestamp 处理类型
   * @param {Boolean} isDetail 是否显示详细时间
   */
  function chatDateFormat (timestamp, isDetail = true) {
    var chatTime = { _date: timestamp || Date.now() }
    chatTime.init = function () {
      var time = getDate(chatTime._date).getTime()
      var now = getDate();
      chatTime._timeStamp = time
      chatTime._now = now.getTime()
      chatTime._nowYear = now.getFullYear()
      chatTime._nowMonth = now.getMonth() + 1
      chatTime._nowDay = now.getDate()
      chatTime._nowHour = now.getHours()
      chatTime._nowMinute = now.getMinutes()
      chatTime._nowSecond = now.getSeconds()
      chatTime._nowWeek = now.getDay()
      chatTime._argYear = getDate(chatTime._date).getFullYear()
      chatTime._argMonth = getDate(chatTime._date).getMonth() + 1
      chatTime._argDay = getDate(chatTime._date).getDate()
      chatTime._argHour = getDate(chatTime._date).getHours()
      chatTime._argMinute = getDate(chatTime._date).getMinutes()
      chatTime._argSecond = getDate(chatTime._date).getSeconds()
      chatTime._argWeek = getDate(chatTime._date).getDay()
    }
    chatTime.week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    chatTime.formatNumber = function (n) {
      n = n.toString()
      return n[1] ? n : '0' + n
    }
    chatTime.timeFrame = function() {
      var geo = [[0, 5], [6, 8], [9, 11], [12, 13], [14, 16], [17, 18], [19, 21], [22, 23]]
      var frame = ['凌晨', '早上', '上午', '中午', '下午', '傍晚', '晚上', '深夜']
      return frame[geo.indexOf(geo.filter(function(v) {
        return v[0] <= chatTime._argHour && v[1] >= chatTime._argHour
      })[0])]
    }
    chatTime.formatDate = function(formatString, isTimeFrame = false){
      function format (e) { return e < 10 ? ('0' + e) : e }
      var _date = getDate(timestamp)
      var time = { timestamp:timestamp }
      time.YYYY = _date.getFullYear()
      time.MM = format(_date.getMonth() + 1)
      time.DD = format(_date.getDate())
      time.hh = format(_date.getHours())
      time.mm = format(_date.getMinutes())
      time.ss = format(_date.getSeconds())
      var res = ''
      formatString.trim().split(' ').forEach(function(value, index){
        if (!value) return
        var l = value.split('-')
        var c = value.split(':')
        if (index) res += ' '
        if (index && isTimeFrame) res += chatTime.timeFrame().toString()
        if (l.length === 1) {
          c.forEach(function(val, idx){
            if (idx) res += ':'
            res += time[val]
          })
          if (!index && isTimeFrame) res = chatTime.timeFrame().toString() + res
        } else {
          l.forEach(function(val, idx){
            if (idx) res += '-'
            res += time[val]
          })
        }
      })
      return res
    }
    chatTime.init()

    if (chatTime._nowYear === chatTime._argYear) {
      if (chatTime._nowMonth === chatTime._argMonth) { // 一个月内
        var dayDiff = chatTime._nowDay - chatTime._argDay
        if (dayDiff > 7) {
          return isDetail ? chatTime.formatDate('MM-DD hh:mm', true) : chatTime.formatDate('MM-DD')
        } else if (dayDiff === 1) {
          return isDetail ? '昨天'+chatTime.formatDate('hh:mm', true) : '昨天' + chatTime.formatDate('hh:mm')
        } else if (dayDiff === 2) {
          return isDetail ? '前天' + chatTime.formatDate('hh:mm', true) : '前天' + chatTime.formatDate('hh:mm')
        } else if ([3, 4, 5, 6, 7].indexOf(dayDiff) != -1) {
          return isDetail ? chatTime.week[chatTime._argWeek] + chatTime.formatDate('hh:mm') : chatTime.week[chatTime._argWeek]
        } else if (dayDiff <= 0) {
          return chatTime.formatDate('hh:mm', true)
        }
      } else if (chatTime._nowMonth > chatTime._argMonth) {
        return isDetail ? chatTime.formatDate('MM-DD hh:mm', true) : chatTime.formatDate('MM-DD')
      } else {
        return isDetail ? chatTime.formatDate('MM-DD hh:mm', true) : chatTime.formatDate('MM-DD')
      }
    } else {
      return chatTime.formatDate('YYYY-MM-DD')
    }
  }
  
  module.exports = {
    chatDateFormat : chatDateFormat
  }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值