各位小伙伴 灵活取用 按需修改~
如有bug,欢迎大佬指正! [ 抱拳 ]
1. 方法和一些方法的调用
getCurrentDate() {
let date = new Date() //直接new Date()得到的时间格式 Thu Dec 09 2021 09:05:45 GMT+0800 (中国标准时间)
let dateTime = date.getTime() //当前时间的时间戳 1639011993780
let dateDay = date.getDay() // 今天是本周的第几天 4
let dateMonth = date.getMonth() //当月是本年的第几个月-1 11
let dateYear = date.getFullYear() // 当前年份 2021
let dateDate = date.getDate() //当前日期 9
let dateH = date.getHours() //当前小时 9
let dateM = date.getMinutes() // 当前分钟
let dateS = date.getSeconds() // 当前秒
let format = this.formatTime(dateTime) //2021-12-09
let formarS = this.formatTimeSS(dateTime) //2021-12-09 09:32:47
let DateString = '2021-12-09'
// 根据日期字符串 得到日期格式
let DateStringToDate = this.stringToDate(DateString) //Thu Dec 09 2021 00:00:00 GMT+0800 (中国标准时间)
//转换为时间格式后得到时间戳
let DateStringToDateTime = this.stringToDate(DateString).getTime() //(也是某一天凌晨时间戳)1638979200000
//根据某天开始时间戳得到最后一秒时间戳
let DateEnd = DateStringToDateTime + 24 * 3600 * 1000 - 1
//根据当前日期 当月第一天凌晨 当月最后一秒时间戳
let getStartEndMonthDay = this.getStartEndMonth(date)
// monthEndDate: Fri Dec 31 2021 00:00:00 GMT+0800 (中国标准时间) {}
// monthEndDateTime: 1640966399999
// monthStartDate: Wed Dec 01 2021 00:00:00 GMT+0800 (中国标准时间) {}
// monthStartDateTime: 1638288000000
// 根据当前日期 获取本周 开始 结束日期时间 周日到周六 凌晨到最后一秒 参数date是日期 如‘2021-12-09’
let getStartAndEndDateOfWeek = this.getStartAndEndDateOfWeek(DateStringToDate)
// endDate: Sat Dec 11 2021 23:59:59 GMT+0800 (中国标准时间) {}
// startDate: Sun Dec 05 2021 00:00:00 GMT+0800 (中国标准时间) {}
},
2. 一些关于时间的处理工具(调用方法 传参 在上面)
- 时间格式化 时间戳 时间转换为YYYY-MM-DD
formatTime(val) {
if (val) {
let date = new Date(val);
let Y = date.getFullYear();
let M = date.getMonth() + 1;
let D = date.getDate();
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
return Y + '-' + M + '-' + D;
} else {
return '';
}
},
- 转换为YYYY-MM-DD HH:MM:SS
formatTimeSS(val) {
if (val) {
let date = new Date(val);
let Y = date.getFullYear();
let M = date.getMonth() + 1;
let D = date.getDate();
let HH = date.getHours();
let MM = date.getMinutes();
let SS = date.getSeconds();
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
if (HH < 10) {
HH = '0' + HH;
}
if (MM < 10) {
MM = '0' + MM;
}
if (SS < 10) {
SS = '0' + SS;
}
return Y + '-' + M + '-' + D + ' ' + HH + ':' + MM + ':' + SS
} else {
return '';
}
},
- 时间字符串 转化为 日期格式
stringToDate(string) {
if (string) {
var arr1 = string.split(" ");
var sdate = arr1[0].split('-');
var date = new Date(sdate[0], sdate[1] - 1, sdate[2]);
return date;
}
},
- 获取某年某月有多少天 返回天数
getCurrentMonthDays(month, year) {
let monthArr = [1, 3, 5, 7, 8, 10, 12];
let days = 0;
if (month == 2) {
if (year % 4 == 0) {
days = 29;
} else {
days = 28;
}
} else if (monthArr.indexOf(month) >= 0) {
days = 31;
} else {
days = 30;
}
return days;
},
- 根据日期获取该月第一天开始 最后一天结束的时间戳
getStartEndMonth(date) {
let M = date.getMonth() + 1,
Y = date.getFullYear(),
D = date.getDate();
let days = this.getCurrentMonthDays(M, Y);
let monthStartDate = new Date(Y, M - 1, 1);
let monthEndDate = new Date(Y, M - 1, days);
let monthStartDateTime = monthStartDate.getTime()
let monthEndDateTime = monthEndDate.getTime() + 86400000 - 1 //当月最后一天最后一秒时间戳
const data ={
monthStartDate,
monthEndDate,
monthStartDateTime,
monthEndDateTime
}
return data
},
- 根据当前日期 获取本周 开始 结束日期时间 时间戳
getStartAndEndDateOfWeek(date) {
let millisecondsOfDay = 24 * 60 * 60 * 1000
let dayOfWeek = date.getDay()
let startDate = new Date(date.getTime() - (dayOfWeek) * millisecondsOfDay)
let endDate = new Date(date.getTime() + (7 - dayOfWeek) * millisecondsOfDay-1)
return {
startDate,
endDate
}
},