日期处理
let date = {
//小于10补零
addZero:function(num){
return num > 9 ? num : "0"+num;
}
//格式化时间
,formatDate:function(date,separate){
if(!typeof separate == 'string' && separate != undefined){
throw new Error("separate "+separate+" is not string type");
}
var separate = separate || '-';
return date.getFullYear() + separate
+ this.addZero(date.getMonth() + 1)+ separate
+ this.addZero(date.getDate())+" "
+ this.addZero(date.getHours())+":"
+ this.addZero(date.getMinutes())+":"
+ this.addZero(date.getSeconds());
}
//获取date
,getDate:function(dateStr){
//日期格式yyyy-MM-dd
let regDate = /^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/
if(regDate.test(dateStr)){
let dateArr = dateStr.split("-")
return new Date(dateArr[0],dateArr[1],dateArr[2],0,0,0)
}
return new Date(dateStr);
}
//获取时间戳
,getTimeStamp:function(date){
return date.getTime();
}
//计算时间差
,timeDiffer:function(beginDateStr,endDateStr){
var startTime = new Date(beginDateStr);
var endTime = new Date(endDateStr);
var date= endTime.getTime() - startTime.getTime();
var days = Math.round(date/(24*3600*1000));
return days;
}
//计算时间差,当beginDateStr,endDateStr相同时,包含这天
,timeDifferInclude:function(beginDateStr,endDateStr){
let days = this.timeDiffer(beginDateStr,endDateStr)+1;
return days;
}
/**
* set多少天前/后的date
* date:可以是(时间戳,格式化时间YYYY-MM-dd HH:mm:ss字符串,Date对象)
*/
,setManyDays:function(date,manyDays){
date = new Date(date)
date.setDate(date.getDate() + manyDays);
return date;
}
//获取指定年、月的天数
,getDays:function(year, month){
return new Date(year,month,0).getDate();
}
//获取某年,某月的第一个天是周几
,getFirstDayIsWeek:function(y,m){
return new Date(y,m-1,1).getDay();
}
//获取年月日
,getYMD:function(date){
return {year:date.getFullYear(),month:date.getMonth()+1,day:date.getDate()}
}
//获取某年某月上个月days天日期
,getPreMonthDays:function(y,m,days){
let num = []
,calNum = this.date.getDays(y,m-1);
for(let i=calNum-days+1; i<=calNum; i++){
num.push(i);
}
return num;
}
/**
* 获取日历数据
* getCalData(Date对象,'prev')--->获取当前年月的前一个月的,日历数据
* getCalData(Date对象,'next')--->获取当前年月的下一个月的,日历数据
* getCalData(Date对象,month)--->获取当年的指定月份的,日历数据
* getCalData(year,month)-->获取2020年8月,日历数据
* */
,getCalData:function(date,month){
month = month||date.getMonth()+1;
let isDate = this.isType('date')
,dateObj = this.date.getYMD(date)
,calData = [];
if(isDate(date)){
month == 'prev' && date.setFullYear(dateObj.year,dateObj.month-1)
month == 'next' && date.setFullYear(dateObj.year,dateObj.month+1)
typeof month == 'number' && (date = new Date(),date.setFullYear(date.getFullYear(),month-1))
}else{
/^\d{4}$/.test(date) && /^([1-9]|1[0-2]{1})$/.test(month) && (date = new Date(date,month-1))
}
let week = this.date.getFirstDayIsWeek(dateObj.year,dateObj.month)
,days = this.date.getDays(dateObj.year,dateObj.month)
,trs = Math.ceil((days+week)/7)
,fullDay = trs*7-week;
//减去week天,就添加这些天
Array.prototype.push.apply(calData,getPreMonthDays(dateObj.year,dateObj.month,week));
Array.prototype.push.apply(calData,this.times(days));
Array.prototype.push.apply(calData,this.times(fullDay-days));
return this.toTwoArray(calData);
}
//dates:['2020-01','2019-04','2020-09']
,getYMDates:function(year,month,dates){
let days = this.date.getDays(new Date(year,month));
let regExpStr = year+"-"+this.date.addZero(month)+'-[0-'+days+']'
, regExp = new RegExp(regExpStr);
return dates.filter(function(date){
return regExp.test(date);
})
}
/**
* 计时器
* 参数:num 毫秒数
*/
/* 例1:convertTime(10*1000)//10秒
* 例2:convertTime(28*60*1000)//28分钟
* 例3:convertTime(2*3600*1000)//2个小时
* 例4:convertTime(2*60*60*1000+28*60*1000+10*1000)//2小时 28分钟 10秒
* 例5:convertTime(2*3600*1000+3600*1000+60*1000)//3小时 1分钟 0秒
*/
,timer:function(num){
num = parseInt(num/1000);
var second = num%60
,minute = parseInt(num/60)%60
,hour = parseInt(num/3600);
return this.addZero(hour)+":"+this.addZero(minute)+":"+this.addZero(second);
}