js 判断某个时间 是 昨天、今天、明天 | 时间范围 |禁用时分秒

一、今天/昨天/明天 判断

#处理后台返回来的日期数据
let activeData = new Date(date) //后台返回的某个时间(格式为 2013-3-10)
activeDataArr = data[i].date.split('-'); //某人时间段的 年月日

# 今天
let todayDate = new Date(); //今天
let nowDataArr = [todayDate.getFullYear(),todayDate.getMonth()+1,todayDate.getDate()] //今天的 年 月 日

# 明天
let tomorrowData = new Date(todayDate.setTime(todayDate.getTime()+24*60*60*1000))//明天
let tomorrowDataArr = [tomorrowData.getFullYear(),tomorrowData.getMonth()+1,tomorrowData.getDate()]//明天的 年 月 日

# 昨天
let yesterdayDate = new Date(todayDate.setTime(todayDate.getTime()-24*60*60*1000))
let yesterdayDateArr = [yesterdayDate.getFullYear(),yesterdayDate.getMonth()+1,yesterdayDate.getDate()]//昨天的 年 月 日

var natian = '' //储存到底是哪天

//判断 是今天还是昨天 还是 明天  并赋值给 natian
if(todayDate[0]== activeDataArr[0]&&todayDate[1]== activeDataArr[1]&&todayDate[2]== activeDataArr[2]) {
	natian = '今天'
}else if(tomorrowDataArr[0]== activeDataArr[0]&&tomorrowDataArr[1]== activeDataArr[1]&&tomorrowDataArr[2]== activeDataArr[2]) {
	natian = '明天'
}else if(yesterdayDateArr[0]== activeDataArr[0]&&yesterdayDateArr[1]== activeDataArr[1]&&yesterdayDateArr[2]== activeDataArr[2]) {
	natian = '昨天'
}

二、获取当月当日到上/下某月 整月(30天)的时间范围

/**
      * 
      * @param dateNow :Date类
      * @param intervalDays :间隔天数
      * @param bolPastTime  :Boolean,判断在参数date之前,还是之后,
      */
    getDateRange(dateNow, intervalDays, bolPastTime) {
      let oneDayTime = 24 * 60 * 60 * 1000;
      let list = [];
      let lastDay;
      if (bolPastTime == true) {//前时间范围
        lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTime);
        list.push(this.formateDate(lastDay));
        list.push(this.formateDate(dateNow));
      } else {//后时间范围
        lastDay = new Date(dateNow.getTime() + intervalDays * oneDayTime);
        list.push(this.formateDate(dateNow));
        list.push(this.formateDate(lastDay));
      }
      return list;
    },
    formateDate(date) {
      console.log(date,'date----date')
      let [yyyy, month, day, week] = [date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getDay()]
      if (month < 10) {month = '0' + month}
      if (day < 10) {day = '0' + day}
      return yyyy + '-' + month + '-' + day + ''
    },

mounted() {
    var date = new Date();
    var list = this.getDateRange(date, 30, true)
    console.log("获取近一个月日期范围:\n开始日期:" + list[0] + ";\n结束日期:" + list[1]);
  },

element ui禁用时分秒

:picker-options="pickerOption"
pickerOption: {
        selectableRange:(()=>{
          let data=new Date(Date.now() + 1000 *60 * 10);//禁用前十分钟
                let hour=data.getHours();
                let minute=data.getMinutes();
                let second=data.getSeconds();
                return [`${hour}:${minute}:${second} - 23:59:59`]
          })(),
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;//禁用今天前的时间
        }
      },

获取月份范围

# 调用
this.getDateRange(3) //[yyyy-当前月-3, yyyy-当前月]
#
getDateRange(num) {
      let list = []
      var timeC = new Date().setMonth(new Date().getMonth() - num);
      console.log(timeC,'timeC')
      var year = new Date(parseInt(timeC)).getFullYear()
      var month = new Date(parseInt(timeC)).getMonth() + 1;
      if (month < 10) { month = '0' + month}
      list.push(year + '-' + month)
      list.push(new Date().getFullYear() + '-' + (new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1 < 10))
      return list;
    },

区分闰年 | 二月 -------时间段

# formatValue: [],//格式化值 ['2022-09', '2022-12']

let value = this.getDateRange(1) //['2022-02', '2022-02']
this.formatDate(value) //2022-01-01 00:00:00,2022-02-28 23:59:59

//数组转字符串
formatDate(arr) {
	let first = arr[0] + '-01 00:00:00'
	let last = this.formateData1(arr[1])
	return `${first},${last}`
},
formateData1(str) {
	let a = str.split('-')[0]
	let b = str.split('-')[1]
	if (b == 2) {
		if ((a % 4 == 0 && (a % 100) != 0) || (a % 400 == 0)) { str = str + '-29 23:59:59' }
		else { str = str + '-28 23:59:59' }
	}
	if (b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12) {
	str = str + '-31 23:59:59'
	} else if (b == 4 || b == 6 || b == 9 || b == 11) {
	str = str + '-30 23:59:59'
	}
	return str
},



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RxnNing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值