Vue 获取最近一个月,前三个月, 最近半年,最近一年, 当前月末日期

/*
* 获取当前年月日
* */
export function nowDate() {
  const loadYear = new Date().getFullYear()
  let loadMonth = new Date().getMonth() + 1
  let loadDay = new Date().getDate()
  if (loadMonth >= 1 && loadMonth <= 9) {
    loadMonth = '0' + loadMonth
  }
  if (loadDay >= 0 && loadDay <= 9) {
    loadDay = '0' + loadDay
  }
  const date = loadYear + '-' + loadMonth + '-' + loadDay
  return date
}
/*
* 获取前三个月
* */
export function beforeThree() {
  const dates = new Date()
  dates.setMonth(dates.getMonth() - 3)
	var pastMonth = dates.getMonth() + 1
	var pastDay = dates.getDate()
	if (pastMonth >= 1 && pastMonth <= 9) {
	  pastMonth = '0' + pastMonth
	}
	if (pastDay >= 0 && pastDay <= 9) {
	  pastDay = '0' + pastDay
	}
  const endDate = dates.getFullYear() + '-' + pastMonth + '-' + pastDay
  return endDate
}
/*
* 最近一月
* */
export function oneMonth() {
  const dates = new Date()
	dates.setMonth(dates.getMonth() - 1)
	var pastMonth = dates.getMonth() + 1
	var pastDay = dates.getDate()
	if (pastMonth >= 1 && pastMonth <= 9) {
	  pastMonth = '0' + pastMonth
	}
	if (pastDay >= 0 && pastDay <= 9) {
	  pastDay = '0' + pastDay
	}
  const endDate = dates.getFullYear() + '-' + pastMonth + '-' + pastDay
  return endDate
}
/*
* 最近半年
* */
export function haflYear() {
  // 先获取当前时间
  var curDate = (new Date()).getTime()
  // 将半年的时间单位换算成毫秒
  var halfYear = 365 / 2 * 24 * 3600 * 1000
  // 半年前的时间(毫秒单位)
  var pastResult = curDate - halfYear
  // 日期函数,定义起点为半年前
  var pastDate = new Date(pastResult)
  var pastYear = pastDate.getFullYear()
  var pastMonth = pastDate.getMonth() + 1
  var pastDay = pastDate.getDate()
	if (pastMonth >= 1 && pastMonth <= 9) {
	  pastMonth = '0' + pastMonth
	}
	if (pastDay >= 0 && pastDay <= 9) {
	  pastDay = '0' + pastDay
	}
  var endDate = pastYear + '-' + pastMonth + '-' + pastDay
  return endDate
}
/*
* 最近一年
* */
export function reactYear() {
  var nowDate = new Date()
  var dates = new Date(nowDate)
  dates.setDate(dates.getDate() - 365)
  var seperator1 = '-'
  var year = dates.getFullYear()
  var month = dates.getMonth() + 1
  var strDate = dates.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate
  return currentdate
}
/*
* 当前月末日期
* */
export function endMonth(loadYear, loadMonth) {
  const d = new Date(loadYear, Number(loadMonth) + 1, 0)
  const endDay = d.getDate()
  const endDate = loadYear + '-' + (Number(loadMonth) + 1) + '-' + endDay
  return endDate
}

/**
 * 当前日期+1
 */
export function getNextDay() {
  const date = new Date();
  date.setTime(date.getTime()+24*60*60*1000);
  const nextDay = {
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    day: date.getDate()
  }
  return nextDay
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值