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.
63 lines
1.4 KiB
63 lines
1.4 KiB
export const haveDateTime =(dateStr:string,num:number,type:boolean) =>{
|
|
let monthnum = 0;
|
|
if(typeof(num) == 'string'){
|
|
monthnum = parseInt(num);
|
|
}else{
|
|
monthnum = num
|
|
}
|
|
|
|
let tadycont = new Date(dateStr)
|
|
//获取原日
|
|
let day = tadycont.getDate();
|
|
//获取原月份
|
|
let month=tadycont.getMonth();
|
|
//设置增加月份
|
|
tadycont.setMonth(tadycont.getMonth() + (monthnum*1), 1);
|
|
//获取增加的后的月份
|
|
let Jmonth = tadycont.getMonth()+1;
|
|
//获取增加的后的年份
|
|
let Jyear=tadycont.getFullYear();
|
|
if(Jmonth == 4 || Jmonth == 6 || Jmonth == 9 || Jmonth == 11) {
|
|
//小月
|
|
if (day > 30) {
|
|
day = 30;
|
|
}
|
|
}else if (Jmonth == 2) {
|
|
//2月判断是否闰年
|
|
if(((Jyear % 4) == 0) && ((Jyear % 100) != 0) || ((Jyear % 400) == 0)){
|
|
if (day > 29) {
|
|
day = 29;
|
|
}
|
|
}else{
|
|
if (day > 28) {
|
|
day = 28
|
|
}
|
|
}
|
|
}else{
|
|
//大月
|
|
if (day > 31) {
|
|
day = 31;
|
|
}
|
|
}
|
|
let tHours = tadycont.getHours();
|
|
let tMinutes = tadycont.getMinutes();
|
|
let tSeconds = tadycont.getSeconds();
|
|
let Jmonthstr = doHandleMonth(Jmonth);
|
|
let daystr = doHandleMonth(day);
|
|
if(!type){
|
|
return Jyear+"-"+Jmonthstr+"-"+daystr;
|
|
}
|
|
return Jyear+"-"+Jmonthstr+"-"+daystr+" "+tHours+":"+tMinutes+":"+tSeconds;
|
|
}
|
|
/**
|
|
* 日或月补0
|
|
* @param month
|
|
* @returns {string}
|
|
*/
|
|
function doHandleMonth(month:number):string {
|
|
let m = month.toString();
|
|
if(month.toString().length == 1){
|
|
m = "0" + month;
|
|
}
|
|
return m;
|
|
}
|
|
|