js月份,日期加一天


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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM