const dateFun = {
/**
* demo:yyyy-MM-dd hh:mm:ss.S
* @param {[type]} fmt [description]
* @param {[type]} date [description]
* @return {[type]} [description]
*/
dateFormat (fmt, date) {
let thisDate = date || new Date()
var o = {
'M+': thisDate.getMonth() + 1, // 月份
'd+': thisDate.getDate(), // 日
'h+': thisDate.getHours(), // 小时
'm+': thisDate.getMinutes(), // 分
's+': thisDate.getSeconds(), // 秒
'q+': Math.floor((thisDate.getMonth() + 3) / 3), // 季度
's': thisDate.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (thisDate.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
},
// 获取当前月的前后三个月,当前是8月,一共展示789三个月
getMonthList (curDate, prev = 1, next = 1) {
let that = this
let all = []
let months = prev + next + 1
for (let i = 0; i < months; i++) {
let date = curDate ? new Date(that.backDate(1, curDate)) : new Date(that.backDate(1))
date.setFullYear(date.getFullYear(), date.getMonth() + i, 1)
let mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
all.push({
month: date.getFullYear() + '-' + mm + '-' + '01',
days: that.getCurrentMonthTableData(date)
})
}
return all
},
// 获取list月份
getCalendarDataByMonth (start, end) { // 开始日期, 结束日期(2019-08-08)
let that = this
let all = []
let months = this.getDiffMonths(start, end) + 1
for (let i = 0; i < months; i++) {
let date = start ? new Date(start) : new Date()
date.setFullYear(date.getFullYear(), date.getMonth() + i, 1)
all.push({
month: date.getFullYear() + '年' + (date.getMonth() + 1) + '月',
days: that.getCurrentMonthTableData(date)
})
}
return all
},
// 或两个日期相差几个月(2019-08-08, 2019-06-30)相差2个月
getDiffMonths (start, end) {
let months = 0
let startList = start.split('-')
let endList = end.split('-')
months = (endList[0] - startList[0]) * 12 + (endList[1] - startList[1])
return months
},
/**
* 时间跳转当前月的前几个月或者后几个月几个月
* curDate格式 2019-01-01格式,step=1上一个月,step=-1下一个月
*
*/
backDate (step, curDate) {
var t = curDate ? new Date(curDate) : new Date()
var dd = new Date(t.setMonth(t.getMonth() - step))
dd = new Date(dd.setDate(dd.getDate() + 1))
return this.dateFormat('yyyy-MM-dd', dd)
},
/**
* 获取查询日期当月的所有日期
* d需要new Date() 格式
* 返回的的格式months=["", "", Wed May 01 2019 00:00:00 GMT+0800 (中国标准时间)])
*/
getCurrentMonthTableData (d) {
let date = d || new Date()
let year = date.getFullYear()
let month = date.getMonth()
date.setFullYear(year, month + 1, 0) // 当前月最后一天
let currentDays = date.getDate() // 当前几号
let finalWeek = date.getDay() // 当月最后一天星期几
date.setDate(1)
let firstWeek = date.getDay() - 1 < 0 ? 6 : date.getDay() - 1 // 当月第一天星期几 前一天星期几
let months = []
while (firstWeek--) {
months.push('')
}
for (let i = 0; i < currentDays; i++) {
months.push(new Date(year, month, i + 1))
}
if (finalWeek <= 6 && finalWeek > 0) {
for (let i = 0, len = 7 - finalWeek; i < len; i++) {
months.push('')
}
}
return months
},
/**
* 获取时间戳
* date = '2019-01-01'
*/
dateConvertSec (date) {
if (date) {
return new Date(date.replace(/-/g, '/')).getTime()
}
return 0
},
/**
* 判断是否是今天
* dateObject = new Date()格式
*/
isToday (dateObject) {
return this.dateFormat('yyyy-MM-dd', dateObject) === this.dateFormat('yyyy-MM-dd')
},
/**
* 获取当月第一天和最后一天
*
*/
getCurrentMounth (mday = null, natural = true) {
let day = mday || new Date()
day.setDate(1) // 当月第一天
let start = this.dateFormat('yyyy-MM-dd', day)
day.setMonth(day.getMonth() + 1) // 下个月
day.setDate(day.getDate() - 1) // 下个月第一天减1得到本月最后一天
let end = natural ? this.dateFormat('yyyy-MM-dd') : this.dateFormat('yyyy-MM-dd', day)
return { start: start, end: end }
},
// 获取上个月的第一天和最后一天
getPreMounth () {
let date = new Date()
date.setDate(1)
date.setMonth(date.getMonth() - 1)
let start = this.dateFormat('yyyy-MM-dd', date)
date.setMonth(date.getMonth() + 1)
date.setDate(date.getDate() - 1)
let end = this.dateFormat('yyyy-MM-dd', date)
return { start: start, end: end }
},
// 获取当年
getCurrentYear (mday = null, natural = true) {
let day = mday || new Date()
day.setDate(1)
day.setMonth(0)
let start = this.dateFormat('yyyy-MM-dd', day)
let end = natural ? this.dateFormat('yyyy-MM-dd') : day.getFullYear() + '-12-31'
return { start: start, end: end }
},
/**
* 获取date的前几天或者后几天的时间
date = '2019-01-01'
* // true表示未来的第几天,false表示过去的第几天
*/
getCurrentDay (queryDate, step = 0, type = true) {
let date = new Date(queryDate)
let dd = null
if (type) {
dd = new Date(date.getTime() + step * 24 * 60 * 60 * 1000)
} else {
dd = new Date(date.getTime() - step * 24 * 60 * 60 * 1000)
}
return this.dateFormat('yyyy-MM-dd', dd)
},
/**
* endDate = '2019-01-01'格式
*
*/
getCurrentWeek (natural = true) {
let day = new Date()
let num = (day.getDay() || 7) - 1
day.setDate(day.getDate() - num) // 本周第一天
let start = this.dateFormat('yyyy-MM-dd', day)
day.setDate(day.getDate() + 6) // 本周最后一天
let end = natural ? this.dateFormat('yyyy-MM-dd') : this.dateFormat('yyyy-MM-dd', day)
return { start: start, end: end }
},
/** 本月累计
* currentDate 2019-09-01 return { start:'2019-08-01',end:'2019-08-31'}
* currentDate 2019-09-02 return { start:'2019-09-01',end:'2019-09-01'}
* currentDate 2019-09-18 return { start:'2019-09-01',end:'2019-09-17'}
*/
getCurrentMounthTotal (currentDate) {
let date = currentDate ? new Date(currentDate) : new Date()
let day = date.getDate()
let yesterday = new Date(this.getCurrentDay(currentDate, 1, false))
if (day === 1) {
return {
start: this.getCurrentMounth(yesterday, false).start,
end: this.getCurrentMounth(yesterday, false).end
}
} else {
return {
start: this.getCurrentMounth(date).start,
end: this.getCurrentDay(currentDate, 1, false)
}
}
},
// 本月累计,
// date 参数可以是 ‘2019/10/10’、'2019-10-10'、'20191010',也可以是日期对象Date()
// num 类型是number, 参数为2时,是当前日期后的8号;默认是2
// 返回 {start: '2019-10-1', end: '2019-10-8' }
dateMonthRange (date, num = 2) {
let D
// 如果传进来的时字符串
if (typeof date === 'string' && date) {
if (date.length === 6) {
date.replace(/(\d{6})(\d{2})(\d{2})/, '$1/$2/$3')
}
// 如果日期中存在‘-’,替换成‘/’,处理一部分浏览器不能识别‘-’
date.replace(/-/g, '/')
D = new Date(date)
// 日期时对象
} else if (typeof date === 'object') {
D = date
} else {
// 没有传日期,就取当天的
D = new Date()
}
let m = D.getDate() - num
D.setDate(m)
let end = this.dateFormat('yyyy-MM-dd', D)
D.setDate(1)
let start = this.dateFormat('yyyy-MM-dd', D)
return {
start: start,
end: end
}
},
// 本年累计,
// date 参数可以是 ‘2019/10/10’、'2019-10-10'、'20191010',也可以是日期对象Date()
// num 类型是number, 参数为2时,是当前日期后的8号;默认是2
// 返回 {start: '2019-1-1', end: '2019-10-8' }
dateYearRange (date, num = 2) {
let D
// 如果传进来的时字符串
if (typeof date === 'string' && date) {
if (date.length === 6) {
date.replace(/(\d{6})(\d{2})(\d{2})/, '$1/$2/$3')
}
// 如果日期中存在‘-’,替换成‘/’,处理一部分浏览器不能识别‘-’
date.replace(/-/g, '/')
D = new Date(date)
// 日期时对象
} else if (typeof date === 'object') {
D = date
} else {
// 没有传日期,就取当天的
D = new Date()
}
let m = D.getDate() - num
D.setDate(m)
let end = this.dateFormat('yyyy-MM-dd', D)
let start = D.getFullYear() + '-01-01'
return {
start: start,
end: end
}
},
/**
* 获取查询日期当月的所有日期
* d需要'2019-11-11' 格式
* 返回的的格式week= '1'
*/
getDayweek (d) {
let date = new Date(d)
let currentDays = date.getDay(date) // 当前几号
if (currentDays === 0) {
return 7
} else {
return currentDays
}
}
}
export default {
...dateFun
}