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.
62 lines
1.4 KiB
62 lines
1.4 KiB
export const calculateDatesMonth =(dateStr:string,num:number,type:string) => {
|
|
let monthnum = num;
|
|
if (typeof (num) == "string"){
|
|
monthnum = parseInt(num);
|
|
}
|
|
let date= new Date(dateStr);
|
|
//获取原日
|
|
let day = date.getDate();
|
|
//获取原月份
|
|
let month=date.getMonth();
|
|
//设置增加月份
|
|
date.setMonth(date.getMonth() + (monthnum*1), 1);
|
|
//获取增加的后的月份
|
|
let Jmonth = date.getMonth()+1;
|
|
//获取增加的后的年份
|
|
let Jyear=date.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 = date.getHours();
|
|
let tMinutes = date.getMinutes();
|
|
let tSeconds = date.getSeconds();
|
|
let Jmonthstr = doHandleMonth(Jmonth);
|
|
let daystr = doHandleMonth(day);
|
|
if(type=="0"){
|
|
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;
|
|
}
|
|
|