/*
* 获取当前年月日
* */
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
}
Vue 获取最近一个月,前三个月, 最近半年,最近一年, 当前月末日期
最新推荐文章于 2024-09-07 14:49:36 发布