网上大多日期处理都是功能不全,所以下定决心做个全的出来
// Date.prototype.Format = function (fmt) {
// var o = {
// "M+": this.getMonth() + 1, //月份
// "d+": this.getDate(), //日
// "h+": this.getHours(), //小时
// "m+": this.getMinutes(), //分
// "s+": this.getSeconds(), //秒
// "q+": Math.floor((this.getMonth() + 3) / 3), //季度
// "S": this.getMilliseconds() //毫秒
// };
// if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.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;
// }
// var s = "{time:" + new Date().Format("yyyy-MM-dd hh:mm:ss")
// console.log(s);
class dateFormater{
constructor(){}
//当前时间format
timeFormat(fmt='yyyy-MM-dd hh:mm:ss'){
let date = new Date()
let o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (let 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;
}
//获取当天当前时间===timeFormat
currentDay(state){
return this.timestampToTime(null,state)
}
//获取昨天日期
getYesterday(){
return this.timestampToTime(this.getDayStartTime()-1)
}
//时间戳转日期
timestampToTime(timestamp,state) {
timestamp=timestamp?parseInt(timestamp):Date.now()
let date = new Date(timestamp)
let Y = date.getFullYear() + '-'
let M = ((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '-'
let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
let h = (date.getHours()<10?'0'+date.getHours() + ':':date.getHours() + ':')
let m = (date.getMinutes()<10?'0'+date.getMinutes() + ':':date.getMinutes() + ':')
let s = (date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds())
return state ? Y + M + D +' '+ h + m + s : Y + M + D
}
//获取今天 或 某一天零点时间戳
getDayStartTime(time){
//time = '2020-12-12' || '1605801599000'
if(!time) return new Date(this.timeFormat('yyyy/MM/dd')).getTime();//零点时间戳
let t = new Date(time).getTime();//转为时间戳
let d = this.timestampToTime(t).replace(/[-]/g,'/');//转为标准时间格式'2020/12/12'
return new Date(this.timeFormat(d)).getTime();//零点时间戳
}
//获取今天 或某一天23:59:59时间戳
getDayEndTime(time){
return this.getDayStartTime(time) + 1000*60*60*24 - 1
}
//获取本周第一天零点时间戳
getWeekStartDay(){
let d=new Date().getDay();//获取今天是本周第几天
return this.timestampToTime(this.getDayStartTime() - (d-1)*1000*3600*24);
}
//获取本周最后一天23:59:59时间戳
getWeekEndDay(){
let d=new Date().getDay();//获取今天是本周第几天
return this.timestampToTime(this.getDayStartTime() + (7-d+1)*1000*3600*24 - 1)
}
//获取本周开始结束日期
getWeek_StartStop(){
return [this.getWeekStartDay(),this.getWeekEndDay()]
}
//获取上周开始结束
getPreWeek_StartStop(){
let n=this.getDayStartTime(new Date(this.getWeekStartDay()).getTime());//本周开始时间戳00:00:00
return [this.timestampToTime(n-7*1000*3600*24),this.timestampToTime(n - 1000)]
}
//获取当月或某月天数
getMouthDays(date){
let y = date?new Date(date).getFullYear():new Date().getFullYear()
let M = date?new Date(date).getMonth():new Date().getMonth()
return new Date(y,M+1,0).getDate()
}
//获取本月开始日期
getMouthStartDay(){
let d = new Date().getDate();//获取今天是本月第几天
let t = this.getDayStartTime() - (d-1)*1000*3600*24;//获取月初时间戳
return this.timestampToTime(t)
}
//获取本月或某月结束日期
getMouthEndDay(date){
let y = date?new Date(date).getFullYear():new Date().getFullYear()
let M = date?new Date(date).getMonth():new Date().getMonth()
return this.timestampToTime(new Date(y,M+1,0).getTime())
}
//获取本月或某月开始、结束的日期
getMouth_StartStop(date){
let y = date?new Date(date).getFullYear():new Date().getFullYear();
let M = date?new Date(date).getMonth():new Date().getMonth();
let day = new Date(y,M+1,0)
let end = day.getTime() + 1000*3600*24 - 1;//获取月最后一天时间戳23:59:59
let count = day.getDate();//获取此月的天数
let start = day.getTime() - (count-1)*1000*3600*24;//获取此月第一天时间戳00:00:00
// console.log(start);
return [this.timestampToTime(start),this.timestampToTime(end)]
}
//获取上月开始和结束日期
getPreMouth_StartStop(){
let y = new Date().getFullYear(),M=new Date().getMonth();
return this.getMouth_StartStop(y+'/'+M)
}
//获取今年或某年第一天00:00:00日期
getFirstDayOfYear(date){
let y = date?new Date(date).getFullYear():new Date().getFullYear()
return this.timestampToTime(new Date(y+'/1/1').getTime())
}
//获取今年或某年最后一天23:59:59日期
getLastDayOfYear(date){
let y = date?new Date(date).getFullYear():new Date().getFullYear()
return this.timestampToTime(new Date(y + 1 +'/1/1').getTime()-1000)
}
//今年 或某年
getYear_StartStop(date){
return[this.getFirstDayOfYear(date),this.getLastDayOfYear(date)]
}
//去年
getPreYear_StartStop(){
let y = new Date().getFullYear()
return[this.getFirstDayOfYear(y-1+''),this.getLastDayOfYear(y-1+'')]
}
//本季度
getCurrentSeason(){
let m = new Date().getMonth() - 0
let t = m<3?0:m<6?1:m<9?2:3
return this.getSeasonsOfYear()[t]
}
//上一季度
getPreSeason(){
let m = new Date().getMonth() - 0
let t = m<3?0:m<6?1:m<9?2:3
return this.getSeasonsOfYear()[t-1<0?0:t-1]
}
//本年 或某年四个季度
getSeasonsOfYear(date){
let y = date?new Date(date).getFullYear():new Date().getFullYear()
let d = this.timestampToTime
return [
[d(new Date(y+'/1/1').getTime()),d(new Date(y+'/4/1').getTime()-1)],
[d(new Date(y+'/4/1').getTime()),d(new Date(y+'/7/1').getTime()-1)],
[d(new Date(y+'/7/1').getTime()),d(new Date(y+'/10/1').getTime()-1)],
[d(new Date(y+'/10/1').getTime()),d(new Date(y+1+'/1/1').getTime()-1)]
]
}
howLongAgo(date){
let timeStamp = date?!isNaN(date)&&(date+'').length>4?new Date(parseInt(date)).getTime():new Date(date).getTime():Date.now()
let now = Date.now();
let a = now - timeStamp;
let m = 60*1000
let h = m*60
let d = h*24
let z = d*7
let M = d*30
let y = d*365
let result = a<=m?'刚刚':a<m*60?parseInt(a/m) + '分钟前':
a<h*24?(parseInt(a/h)<1?1:parseInt(a/h))+'小时前':
a<d*7?(parseInt(a/d)<1?1:parseInt(a/d))+'天前':
a<z*4?(parseInt(a/z)<1?1:parseInt(a/z))+'周前':
a<M*12?(parseInt(a/M)<1?1:parseInt(a/M))+'个月前':(parseInt(a/y)<1?1:parseInt(a/y))+'年前'
return result
}
}
let date = new dateFormater()
console.log('默认获取当天',date.currentDay());
console.log('默认获取当天',date.timestampToTime());
console.log('默认获取当天',date.timestampToTime(null,true));
// console.log(new Date('2020', '9','1'))
// console.log( (new Date('2020',5,1) - new Date('10',3,1))/(3600*1000*24))
console.log('日期1',date.timeFormat('yyyy/MM/dd hh-mm-ss'));
console.log('日期2',date.timeFormat('yyyy-MM-dd hh-mm-ss'));
console.log('日期3',date.timeFormat('yyyy-MM-dd hh:mm:ss'));
console.log('非标准时间格式取到8:00:00--->',date.timestampToTime(new Date(date.timeFormat('yyyy-MM-dd')).getTime(),true));
console.log('标准时间格式取到00:00:00---->',date.timestampToTime(new Date(date.timeFormat('yyyy/MM/dd')).getTime(),true));
console.log('时间戳转日期',date.timestampToTime('1606060799999',false));
console.log('时间戳转日期',date.timestampToTime('2015',true));
console.log('今天零点时间戳',date.getDayStartTime());
console.log('某天零点时间戳',date.getDayStartTime('2020-12-5'));
console.log('今天23:59:59时间戳',date.getDayEndTime());
console.log('某天23:59:59时间戳',date.getDayEndTime('2020-12-5'));
console.log('今天=====>',date.currentDay());
console.log('昨天=====>',date.getYesterday());
console.log('本周开始',date.getWeekStartDay());
console.log('本周结束',date.getWeekEndDay());
console.log('本周开始-结束',date.getWeek_StartStop());
console.log('上周开始-结束',date.getPreWeek_StartStop());
console.log('当前月天数',date.getMouthDays());
console.log('某年某月天数',date.getMouthDays('2015/8'));
console.log('本月开始',date.getMouthStartDay());
console.log('本月结束',date.getMouthEndDay());
console.log('本月开始-结束',date.getMouth_StartStop());
console.log('某年某月开始-结束',date.getMouth_StartStop('2005/5'));
console.log('上一月',date.getPreMouth_StartStop());
console.log('今年第一天',date.getFirstDayOfYear());
console.log('某年第一天',date.getFirstDayOfYear('2015-10-21'));
console.log('今年最后一天',date.getLastDayOfYear());
console.log('某年最后一天',date.getLastDayOfYear('2015'));
console.log('今年开始-结束',date.getYear_StartStop());
console.log('去年开始-结束',date.getPreYear_StartStop());
console.log('某年开始-结束',date.getYear_StartStop('2003'));
console.log('今年四个季度',date.getSeasonsOfYear());
console.log('某年四个季度',date.getSeasonsOfYear('2003/12/8'));
console.log('当前季度',date.getCurrentSeason());
console.log('上一季度',date.getPreSeason());
console.log('多久以前-->',date.howLongAgo());
console.log('多久以前-->',date.howLongAgo(Date.now()-10*10000));
console.log('多久以前-->',date.howLongAgo(Date.now()-60*1000*60*2));
console.log('多久以前-->',date.howLongAgo(Date.now()-60*1000*60*24));
console.log('多久以前-->',date.howLongAgo(Date.now()-60*1000*60*24*7*2));
console.log('多久以前-->',date.howLongAgo(Date.now()-60*1000*60*24*7*2*3));
console.log('多久以前-->',date.howLongAgo('1286845932000'));
console.log('多久以前-->',date.howLongAgo('2015'));