You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
4.4 KiB
165 lines
4.4 KiB
const happenTime = (num) => { // 时间戳转换成 “yyyy-MM-dd hh:mm:ss”格式
|
|
let date = new Date(num);
|
|
//时间戳为10位需*1000,时间戳为13位的话不需乘1000
|
|
//年
|
|
let year = date.getFullYear();
|
|
|
|
// 月
|
|
let month = date.getMonth() + 1;
|
|
month = month < 10 ? ('0' + month) : month; //月补0
|
|
|
|
//日
|
|
let day = date.getDate();
|
|
day = day < 10 ? ('0' + day) : day; //天补0
|
|
|
|
//时
|
|
let hours = date.getHours();
|
|
hours = hours < 10 ? ('0' + hours) : hours; //小时补0
|
|
|
|
// 分
|
|
let minutes = date.getMinutes();
|
|
minutes = minutes < 10 ? ('0' + minutes) : minutes; //分钟补0
|
|
|
|
// 秒
|
|
let seconds = date.getSeconds();
|
|
seconds = seconds < 10 ? ('0' + seconds) : seconds; //秒补0
|
|
|
|
// 星期
|
|
const weekArr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
|
|
let weekDay = weekArr[date.getDay()];
|
|
let weekNum = date.getDay();
|
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds} ${weekDay}`
|
|
}
|
|
|
|
const happenTimeJson = (num) => { // 时间戳转换成 对象格式
|
|
/**
|
|
* day: number>10 ? number : "'0'+num"
|
|
* hours: number>10 ? number : "'0'+num"
|
|
* minutes: number>10 ? number : "'0'+num"
|
|
* month: number>10 ? number : "'0'+num"
|
|
* seconds: number>10 ? number : "'0'+num"
|
|
* weekDay: '' // 星期一 ~ 星期日
|
|
* weekNum:number 0 ~ 6
|
|
* year:number
|
|
*/
|
|
let date = new Date(num);
|
|
//时间戳为10位需*1000,时间戳为13位的话不需乘1000
|
|
//年
|
|
let year = date.getFullYear();
|
|
|
|
// 月
|
|
let month = date.getMonth() + 1;
|
|
month = month < 10 ? ('0' + month) : month; //月补0
|
|
|
|
//日
|
|
let day = date.getDate();
|
|
day = day < 10 ? ('0' + day) : day; //天补0
|
|
|
|
//时
|
|
let hours = date.getHours();
|
|
hours = hours < 10 ? ('0' + hours) : hours; //小时补0
|
|
|
|
// 分
|
|
let minutes = date.getMinutes();
|
|
minutes = minutes < 10 ? ('0' + minutes) : minutes; //分钟补0
|
|
|
|
// 秒
|
|
let seconds = date.getSeconds();
|
|
seconds = seconds < 10 ? ('0' + seconds) : seconds; //秒补0
|
|
|
|
// 星期
|
|
const weekArr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
|
|
let weekDay = weekArr[date.getDay()];
|
|
let weekNum = date.getDay();
|
|
return {
|
|
year,
|
|
month,
|
|
day,
|
|
hours,
|
|
minutes,
|
|
seconds,
|
|
weekDay,
|
|
weekNum,
|
|
}
|
|
}
|
|
|
|
const timeProcessing = (time) => { // “yyyy-MM-dd hh:mm:ss”转换成时间戳
|
|
/**
|
|
* 未接收到参数时@return 当前时间
|
|
*/
|
|
let Time = time ? new Date(time) : new Date();
|
|
|
|
// console.log(Time) //Fri Nov 19 2021 01:30:00 GMT+0800 (中国标准时间)
|
|
let timestemp = Time.getTime();
|
|
// console.log(timestemp)
|
|
//1637256600000
|
|
return timestemp
|
|
}
|
|
|
|
//时间是否在本周
|
|
const isThisWeek = (time) => {
|
|
// 当前时间 年月日
|
|
const nowTime = happenTimeJson(timeProcessing())
|
|
let timestamp = timeProcessing(`${nowTime.year}-${nowTime.month}-${nowTime.day} 00:00:00`);
|
|
//本周周日的的时间
|
|
/**
|
|
* (serverDate.getDay() === 0 ? 7 :serverDate.getDay() )
|
|
* 因为星期天为0 所以需要装换为7
|
|
* 用8减,是因为这周结束要到下周一的0点
|
|
*/
|
|
let sundayTiem = timestamp + ((8 - (nowTime.weekNum === 0 ? 7 : nowTime.weekNum)) * 24 * 60 * 60 * 1000)
|
|
// 本周周一的时间
|
|
let mondayTime = timestamp - (((nowTime.weekNum === 0 ? 7 : nowTime.weekNum) - 1) * 24 * 60 * 60 * 1000)
|
|
// 接收参数时间
|
|
let currentData = timeProcessing(time);
|
|
if (currentData > mondayTime && currentData < sundayTiem) {
|
|
/**
|
|
* isweek=true是本周 false不是本周
|
|
*/
|
|
return {
|
|
isweek: true,
|
|
date: happenTimeJson(currentData)
|
|
}
|
|
} else {
|
|
return {
|
|
isweek: false,
|
|
date: happenTimeJson(currentData)
|
|
}
|
|
}
|
|
}
|
|
|
|
//时间是否在今天
|
|
const isThisToday = (time) => {
|
|
// 当前时间
|
|
let timestamp = timeProcessing();
|
|
let serverDate = happenTimeJson(timestamp);
|
|
// 今天开始时间
|
|
let startTiem =timeProcessing(`${serverDate.year}-${serverDate.month}-${serverDate.day-1} 00:00:00`);
|
|
// 今天结束时间
|
|
let endTime = timeProcessing(`${serverDate.year}-${serverDate.month}-${serverDate.day+1} 00:00:00`);
|
|
// 接收参数时间
|
|
let currentData = timeProcessing(time);
|
|
|
|
if (currentData > startTiem && currentData < endTime) {
|
|
/**
|
|
* isweek=true是今天 false不是今天
|
|
*/
|
|
return {
|
|
isweek: true,
|
|
date: happenTimeJson(currentData)
|
|
}
|
|
} else {
|
|
return {
|
|
isweek: false,
|
|
date: happenTimeJson(currentData)
|
|
}
|
|
}
|
|
}
|
|
|
|
export default {
|
|
timeProcessing,
|
|
happenTimeJson,
|
|
happenTime,
|
|
isThisWeek,
|
|
isThisToday
|
|
}
|