获取系统时间,并于选择的时间比较

方法一:
//获取系统时间
		gettime() {
			let now = new Date();
			let year = now.getFullYear().toString(); //得到年份
			let month = (now.getMonth() + 1).toString(); //得到月份
			let date = now.getDate().toString(); //得到日期
			let hour = now.getHours().toString(); //得到小时
			let minute = now.getMinutes().toString(); //得到分钟
			let second = now.getSeconds().toString(); //得到分钟
			if (month.length < 2) {
				month = '0' + month;
			}
			if (date.length < 2) {
				date = '0' + date;
			}
			return year + '-' + month + '-' + date + ' '+hour+':'+minute+':'+second;
		},


		方法二:
		// 时间工具

  // 原型链方式创建方法
  Date.prototype.formatDate = function (fmt = 'yyyy-MM-dd') {
    let o = {
      'M+': this.getMonth() + 1, // 月份
      'd+': this.getDate(), // 日
      'h+': this.getHours(), // 小时
      'm+': this.getMinutes(), // 分
      's+': this.getSeconds(), // 秒
      'q+': Math.floor((this.getMonth() + 3) / 3), //季度
      'S': this.getMilliseconds() //毫秒
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
    return fmt
  }

  /**
   * 得到本季度开始的月份
   * @param month 需要计算的月份
   */
  function getQuarterSeasonStartMonth (month) {
    const spring = 0 //春 
    const summer = 3 //夏 
    const fall = 6 //秋 
    const winter = 9 //冬 
    // 月份从0-11 
    if (month < summer) {
      return spring
    } else if (month < fall) {
      return summer
    } else if (month < winter) {
      return fall
    } else {
      return winter
    }
  }

  /**
   * 获得该月的天数
   * @param year年份
   * @param month月份
   */
  function getMonthDays (year, month) {
    // 本月第一天 1-31 
    const relativeDate = new Date(year, month, 1)
    // 获得当前月份 0-11 
    let relativeMonth = relativeDate.getMonth()
    // 获得当前年份4位年 
    let relativeYear = relativeDate.getFullYear()

    // 当为12月的时候年份需要加1 
    // 月份需要更新为0 也就是下一年的第一个月 
    if (relativeMonth == 11) {
      relativeYear++
      relativeMonth = 0
    } else {
      // 否则只是月份增加,以便求的下一月的第一天 
      relativeMonth++
    }
    // 一天的毫秒数 
    const millisecond = 1000 * 60 * 60 * 24
    // 下月的第一天 
    var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1)
    // 返回得到上月的最后一天,也就是本月总天数 
    return new Date(nextMonthDayOne.getTime() - millisecond).getDate()
  }

  // 获取2个日期相差是否超过1年, 超过1年: true, 未超过一年: false
  export const getIsDiffOneYear = (startDateStr, endDateStr) => {
    if (!startDateStr || !endDateStr) return false
    let diffDay = (new Date(startDateStr).getTime() - new Date(endDateStr).getTime()) / (24 * 60 * 60 * 1000)
    // 加1才是真正的天数
    diffDay = Math.abs(diffDay) + 1
    // 不是闰年
    if (diffDay !== 366) {
      return diffDay > 365
    }
    // 有可能是闰年
    const startYear = new Date(startDateStr).getFullYear()
    const endYear = new Date(endDateStr).getFullYear()
    // 开始年和结束年不是同一年, 直接返回true(已超过一年)
    if (startYear !== endYear) return true
    // 闰年, diffDay = 366
    return !((startYear % 4 === 0 && startYear % 100 !== 0 ) || startYear % 400 === 0)
  }

  // 前一天, dayNumber传入几, 就是前几天
  export const getPrevDay = (fmt = 'yyyy-MM-dd', currentDate, dayNumber = 1) => {
    if (!currentDate) {
      currentDate = new Date()
    }
    const prevDate = new Date(currentDate.getTime() - dayNumber * 24 * 60 * 60 * 1000)
    return prevDate.formatDate(fmt)
  }

  // 今日
  export const getCurrentDay = (fmt = 'yyyy-MM-dd', dayTime) => {
    let day = null
    if (dayTime) {
      day = new Date(dayTime)
    } else {
      day = new Date()
    }
    return day.formatDate(fmt)
  }

  // 本周
  export const getCurrentWeek = (showTime = false) => {
    const now = new Date()
    const weekFirstDay = new Date(now- ((now.getDay() - 1) < 0 ? 6 : (now.getDay() - 1)) * 86400000)
    let firstMonth = Number(weekFirstDay.getMonth()) + 1
    let firstDay = weekFirstDay.getDate()
    const weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)
    let lastMonth = Number(weekLastDay.getMonth()) + 1
    let lastDay = weekLastDay.getDate()

    if (firstMonth < 10) {
      firstMonth = '0' + firstMonth
    }
    if (firstDay < 10) {
      firstDay = '0' + firstDay
    }
    let currentWeekStart = weekFirstDay.getFullYear() + '-' + firstMonth + '-' + firstDay

    if (lastMonth < 10) {
      lastMonth = '0' + lastMonth
    }
    if (lastDay < 10) {
      lastDay = '0' + lastDay
    }
    let currentWeekEnd =  weekLastDay.getFullYear() + '-' + lastMonth + '-' + lastDay
    if (showTime) {
      currentWeekStart += ' 00:00:00'
      currentWeekEnd += ' 23:59:59'
    }
    return [currentWeekStart, currentWeekEnd]
  }

  // 本月
  export const getCurrentMonth = (showTime = false) => {
    const day = new Date()
    day.setDate(1)
    let currentMonthStart = day.formatDate()
    day.setMonth(day.getMonth() + 1) //这时候day已经变成下个月第一天
    day.setDate(day.getDate() - 1) //下个月的第一天的前一天就是本月最后一天
    let currentMonthEnd = day.formatDate()
    if (showTime) {
      currentMonthStart += ' 00:00:00'
      currentMonthEnd += ' 23:59:59'
    }
    return [currentMonthStart, currentMonthEnd]
  }

  // 本季度
  export const getCurrentSeason = (showTime = false) => {
    // 获取当前时间 
    const currentDate = new Date()
    // 获得当前月份 0-11 
    const currentMonth = currentDate.getMonth()
    // 获得当前年份4位年 
    const currentYear = currentDate.getFullYear()
    // 获得本季度开始月份 
    const quarterSeasonStartMonth = getQuarterSeasonStartMonth(currentMonth)
    // 获得本季度结束月份 
    const quarterSeasonEndMonth = quarterSeasonStartMonth + 2

    // 获得本季度开始的日期 
    let quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1)
    quarterSeasonStartDate = quarterSeasonStartDate.formatDate()
    // 获得本季度结束的日期 
    let quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, getMonthDays(currentYear, quarterSeasonEndMonth))
    quarterSeasonEndDate = quarterSeasonEndDate.formatDate()

    if (showTime) {
      quarterSeasonStartDate += ' 00:00:00'
      quarterSeasonEndDate += ' 23:59:59'
    }
    return [quarterSeasonStartDate, quarterSeasonEndDate]
  }

  // 本年
  export const getCurrentYear = (showTime = false) => {
    // 获取当前时间 
    const currentDate = new Date()
    // 获得当前年份4位年 
    const currentYear = currentDate.getFullYear()

    // 本年第一天 
    let currentYearFirstDate = new Date(currentYear, 0, 1)
    currentYearFirstDate = currentYearFirstDate.formatDate()

    // 本年最后一天 
    let currentYearLastDate = new Date(currentYear, 11, 31)
    currentYearLastDate = currentYearLastDate.formatDate()

    if (showTime) {
      currentYearFirstDate += ' 00:00:00'
      currentYearLastDate += ' 23:59:59'
    }
    return [currentYearFirstDate, currentYearLastDate]
  }

  // 原型链方式创建方法
  Date.prototype.formatDate = function (fmt = 'yyyy-MM-dd') {
    let o = {
      'M+': this.getMonth() + 1, // 月份
      'd+': this.getDate(), // 日
      'h+': this.getHours(), // 小时
      'm+': this.getMinutes(), // 分
      's+': this.getSeconds(), // 秒
      'q+': Math.floor((this.getMonth() + 3) / 3), //季度
      'S': this.getMilliseconds() //毫秒
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
    return fmt
  }

  // 格式化时间
  export const formatDateTime = (dateTime, fmt = 'yyyy-MM-dd') => {
    if (!dateTime) return ''
    try {
      return dateTime.formatDate(fmt)
    } catch (e) {
      return ''
    }
  }
getCurrentDay('MM-dd')
		

//用到uniapp的时间选择器
		// 筛选开始时间
		datePicker1(e) {
			let setTime=this.gettime()
			let temporarily = '';
			let temporarily2 = '';
			let electTime = e.target.value.split('-');
			let electTime2 = setTime.split('-');
			for (let j = 0; j < electTime.length; j++) {
				temporarily += electTime[j];
			}
			for (let i = 0; i < electTime2.length; i++) {
				temporarily2 += electTime2[i];
			}
			if (Number(temporarily) > Number(temporarily2)) {
				uni.showToast({
					title: '选择时间错误',
					duration: 1200,
					icon: 'none'
				});
				console.log('错误');
			} else {
				this.date1 = e.target.value;
			}
		},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值