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
},



你可能感兴趣的:(js,js,date)