js沒有直接可以用的函數,所以只能自己寫,其中需要涉及到每個月天數的判斷,如果是2月份的話,還要涉及到閏年的判斷
1 var addDate = { 2 //日期,在原有日期基礎上,增加days天數,默認增加1天 3 setDate:function(date, days){ 4 if (days == undefined || days == '') { 5 days = 1; 6 } 7 var date = new Date(date); 8 date.setDate(date.getDate() + days); 9 var month = date.getMonth() + 1; 10 var day = date.getDate(); 11 return date.getFullYear() + '-' + this.getFormatDate(month) + '-' + this.getFormatDate(day); 12 }, 13 //日期月份/天的顯示,如果是1位數,則在前面加上'0' 14 getFormatDate:function(arg){ 15 if (arg == undefined || arg == '') { 16 return ''; 17 } 18 var re = arg + ''; 19 if (re.length < 2) { 20 re = '0' + re; 21 } 22 return re; 23 } 24 }