小程序循环push截取判断时间显示今天、昨天、上年、凌晨、上午、中午等

微信小程序截取时间判断显示今天昨天上年

截取后台返回的数组截取时间判断显示 今天的数据显示今天 昨天的数据显示昨天 其余的显示月份和日期 不是今年的就显示年月日 这里要用到系统时间和循环截取判断

	var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    console.log(Y + '-' + M + '-' + D );
    var cusxin = []
        var suxsin = []
        var xusinx = []
        var scisxn = []
        for (let i = 0; i < that.data.numsxin.length; i++) {
          scisxn.push(that.data.numsxin[i].createDate.substring(0, 10))
          cusxin.push(that.data.numsxin[i].createDate.substring(5, 10))
          suxsin.push(that.data.numsxin[i].createDate.substring(11, 16))
          xusinx.push(that.data.numsxin[i].createDate.substring(11, 13))
        }
        for (let x = 0; x < scisxn.length; x++) {
          if (scisxn[x] == (Y + '-' + M + '-' + D)) {
            cusxin[x] = '今天'
          } else if (scisxn[x] == (Y + '-' + M + '-' + (D - 1))) {
            cusxin[x] = '昨天'
          } else if ((scisxn[x].substring(0, 4)) != Y) {
            cusxin[x] = scisxn[x]
            console.log('执行了')
          }
        }
        for (let j = 0; j < xusinx.length; j++) {
          if (xusinx[j] > 18) {
            xusinx[j] = '晚上'
          } else if (14 <= xusinx[j] && xusinx[j] < 19) {
            xusinx[j] = '下午'
          } else if (12 <= xusinx[j] && xusinx[j] < 14) {
            xusinx[j] = '中午'
          } else if (6 <= xusinx[j] && xusinx[j] < 12) {
            xusinx[j] = '早上'
          } else if (0 <= xusinx[j] && xusinx[j] < 6) {
            xusinx[j] = '凌晨'
          }
        }
        console.log('年月份', scisxn)
        console.log('日期', cusxin)
        console.log('时间:', suxsin)
        console.log('时间段', xusinx)

有什么问题欢迎评论留言,我会及时回复你的

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值