import dayjs from 'dayjs'
const days = [
'星期天',
'星期一',
'星期二',
'星期三',
'星期四',
'星期五',
'星期六'
]
// 获取星期 number为0-6
export const getDay = (number, str = '') => {
let daysDefault =
str == 'xq'
? days
: ['周天', '周一', '周二', '周三', '周四', '周五', '周六']
return daysDefault[number]
}
export const timeFormat = (date) => {
let date1 = dayjs(date).format('YYYY年MM月DD日')
let date2 = getDay(dayjs(date).day())
let date3 = dayjs(date).format('HH:mm:ss')
return date1 + ' ' + date2 + ' ' + date3
}
export const timeFormat1 = (date) => {
let date1 = dayjs(date).format('YYYY年MM月DD日')
let date3 = dayjs(date).format('HH:mm:ss')
return date1 + ' ' + date3
}
// 通过时间获取几号
export const getDate = (date) => {
return dayjs(date).date()
}
// 通过时间获取月份
export const getMonth = (date) => {
return dayjs(date).month() + 1
}
// 获取格式化年月日
export const getYearMothDay = (date, f) => {
return dayjs(date).format(`YYYY${f}MM${f}DD`)
}
// 获取格式化月日年
export const getMothDayYear = (date, f) => {
return dayjs(date).format(`MM${f}DD${f}YYYY`)
}
// 获取格式化年月
export const getYearMoth = (date, f) => {
return dayjs(date).format(`YYYY${f}MM`)
}
// 获取周几
/**
*
* @param {*} date
* @param {*} str '' 周几 | 'xq' 星期
* @returns
*/
export const getToday = (date, str = '') => {
return getDay(dayjs(date).day(), str)
}
// 得到时分秒
export const getHourMinuteSecond = (date, f) => {
return dayjs(date).format('HH:mm:ss')
}
function getLastDay(year, month) {
var d = new Date(0)
if (month == 12) {
d.setUTCFullYear(year + 1)
d.setUTCMonth(0)
} else {
d.setUTCFullYear(year)
d.setUTCMonth(month)
}
d.setTime(d.getTime() - 1)
return d.getUTCDate()
}
// 获取传进来的日期的月份最后一天
export const getYearMothLastDay = (date) => {
let y = new Date(date).getFullYear()
let m = new Date(date).getMonth() + 1
let d = new Date(0)
if (m == 12) {
d.setUTCFullYear(y + 1)
d.setUTCMonth(0)
} else {
d.setUTCFullYear(y)
d.setUTCMonth(m)
}
d.setTime(d.getTime() - 1)
return y + '-' + (m + '').padStart(2, '0') + '-' + d.getUTCDate()
}