前端 js 日期相关

 获取几天星期几

let d = new Date()

this.wToday = d.getDay()

0~6 表示 周天 ~ 周一

 

截取日期中的某部分:可以使用substring 方法提取字符串中介于两个指定下标之间的字符。

方法形式:substring(start,end)

举例:截取年月日部分的日期,代码如下:

let time= '2022-03-25 13:56:56'
console.log(time.substring(0,10));
// time  2022-03-35

获取系统当前日期:

var dateTime = new Date();
// 年
var year = dateTime.getFullYear();
// 月
var month = dateTime.getMonth();
// 日
var day  = dateTime.getDate();
// 小时
var hh  = dateTime.getHours();
// 分钟
var mm = dateTime.getMinutes();
// 秒
var ss = dateTime.getSeconds();

// 补零
    if (month < 10) {
      month = '0' + month 
    }
    if (day  < 10) {
      day  = '0' + day  
    }
    if (day < 10) {
      day = '0' + day
    }
    if (hh < 10) {
      hh = '0' + hh
    }
    if (mm < 10) {
      mm = '0' + mm
    }
    if (ss < 10) {
      ss = '0' + ss
    }

// 拼接时间,转换格式为:yyyy-mm-dd hh:mm
let Todayime = year + '/' + month + '/' + day + ' ' + hh + ':' + mm +':' + ss

处理时间为今日、次日

// 预计上门取件开始时间
          let pickupStartTime= 2022-05-6 13:56:56
          // 预计上门取件结束时间
          let pickupEndTime= 2022-05-6 18:50:00
          // 获取当前日期
          const date = new Date()
          let y = date.getFullYear() // 年
          let m = date.getMonth() + 1 // 月
          let d = date.getDate() // 日
          // 截取时间,转换格式为:yyyy-mm-dd hh:mm
          let time = new Date(dd)
          let year = time.getFullYear()
          let month = time.getMonth() + 1
          let day = time.getDate()
          let hh = time.getHours()
          let mm = time.getMinutes()
          let timea = new Date(aa)
          let hha = timea.getHours()
          let mma = timea.getMinutes()

          // 补零
          if (m < 10) {
            m = '0' + m
          }
          if (d < 10) {
            d = '0' + d
          }
          if (month < 10) {
            month = '0' + month
          }
          if (day < 10) {
            day = '0' + day
          }
          if (hh < 10) {
            hh = '0' + hh
          }
          if (mm < 10) {
            mm = '0' + mm
          }
          if (hha < 10) {
            hha = '0' + hha
          }
          if (mma < 10) {
            mma = '0' + mma
          }

          console.log('d哈哈哈哈d',year,month,day,hh,mm)

          // 判断为当天日期时不显示年月日,使用此转换,在iOS手机上显示的是NaN
          this.lastTime = hh + ':' + mm // 拼接时间字符串 截止时间
          this.startTime = hha + ':' + mma // 拼接时间字符串 开始时间

          if (year + '-' + month + '-' + day === y + '-' + m + '-' + d) {
            this.todayOrter = 0 //今日
            console.log('今日',this.lastTime,this.startTime)
          } else {
            this.todayOrter = 1 // 次日
          }
        }


IOS 上 Date 对象获取任意时间参数比如 getDay、getTime 都为 NaN,是因为 IOS 的 Date 构造函数不支持 2018-04-26 这种格式的日期,必须转换为 2018/04/26 这种格式才会显示正常;


           // 截取时间
          let t1 = aa.split(' ')[1]
          let t2 = dd.split(' ')[1]
          this.startTime = t1.substring(0, t1.length - 3) // 拼接时间字符串
          this.lastTime = t2.substring(0, t2.length - 3) // 拼接时间字符串

          // this.lastTime = hh + ':' + mm // 拼接时间字符串
          // this.startTime = hha + ':' + mma // 拼接时间字符串

          if (year + '-' + month + '-' + day === y + '-' + m + '-' + d) {
            this.todayOrter = 0
          } else {
            this.todayOrter = 1
          }





// 处理聊天时间
  // conversationList 聊天列表
  // newLastTime 新加入列表的发送时间
  // status 为0表示新消息和首次拉取的消息记录\为1表示继续拉取历史消息的状态
  getTime(conversationList, newLastTime, status) {
    let len = conversationList.length // 聊天记录长度
    let oldLastTime = 0 // 历史聊天列表中最后一条消息记录的时间
    // 判断是否存在历史记录
    if (len > 0) {
      if (status) {
        // 如果存在记录, 获取列表第一条记录的时间
        oldLastTime = conversationList[0].sentTime
      } else {
        // 如果首次存在记录, 获取列表最后一条记录的时间
        oldLastTime = conversationList[len - 1].sentTime
      }
    } else {
      // 如果不存在,设置为0
      oldLastTime = 0
    }
    // 获取当前日期
    let today = new Date()
    let y = today.getFullYear() // 年
    let m = today.getMonth() + 1 // 月
    let d = today.getDate() // 日
    // 获取最新一条聊天记录的时间,转换格式为:yyyy-mm-dd hh:mm
    let time = new Date(newLastTime)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let day = time.getDate()
    let hh = time.getHours()
    let mm = time.getMinutes()

    // 补零
    if (m < 10) {
      m = '0' + m
    }
    if (d < 10) {
      d = '0' + d
    }
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    if (hh < 10) {
      hh = '0' + hh
    }
    if (mm < 10) {
      mm = '0' + mm
    }
    let lastTime = '' // 拼接时间字符串
    // 判断历史聊天记录最后一条消息和最新一条聊天记录之间的时间差距大于等于5分钟时显示时间(5*60s=300s)
    if ((newLastTime - oldLastTime) / 1000 >= 300) {
      // 判断为当天日期时不显示年月日
      if (year + '-' + month + '-' + day === y + '-' + m + '-' + d) {
        lastTime = hh + ':' + mm
      } else {
        lastTime = year + '/' + month + '/' + day + ' ' + hh + ':' + mm
      }
    } else {
      // 小于5分钟 则不显示消息时间
      lastTime = ''
    }
    return lastTime
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值