摘要:JS获取前一个月时间(代码实例)JS获取前一个月时间(代码实例)

小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

JS获取前一个月时间(代码实例)

 找了很久没找到,干脆自己写一个。    function getPreMonthDate() {     let times = new Date();     let Y = times.getFullYear();     let M = times.getMonth() + 1;     let D = times.getDate();     let MD = "";     let dateArr = [];     for (let i = 0; i <= 30; i++) {       if (D - i > 0) {         MD = M + "/" + (D - i);         dateArr[i] = MD;       } else {         M = times.getMonth();         if (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12) {           MD = M + "/" + (D - i + 31);         } else if (M == 4 || M == 6 || M == 9 || M == 11) {           MD = M + "/" + (D - i + 30);         } else {           if (((Y % 4) == 0) && ((Y % 100) != 0) || ((Y % 400) == 0)) {   //闰年             MD = M + "/" + (D - i + 29);           } else {             MD = M + "/" + (D - i + 28);           }         }         dateArr[i] = MD;       }     }     console.log(dateArr);   }   getPreMonthDate();      //结果是否你想要的呢?      /*[ '5/19',     '5/18',     '5/17',     '5/16',     '5/15',     '5/14',     '5/13',     '5/12',     '5/11',     '5/10',     '5/9',     '5/8',     '5/7',     '5/6',     '5/5',     '5/4',     '5/3',     '5/2',     '5/1',     '4/30',     '4/29',     '4/28',     '4/27',     '4/26',     '4/25',     '4/24',     '4/23',     '4/22',     '4/21',     '4/20',     '4/19' ] */  

JS获取前一个月时间(代码实例)

 找了很久没找到,干脆自己写一个。    function getPreMonthDate() {     let times = new Date();     let Y = times.getFullYear();     let M = times.getMonth() + 1;     let D = times.getDate();     let MD = "";     let dateArr = [];     for (let i = 0; i <= 30; i++) {       if (D - i > 0) {         MD = M + "/" + (D - i);         dateArr[i] = MD;       } else {         M = times.getMonth();         if (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12) {           MD = M + "/" + (D - i + 31);         } else if (M == 4 || M == 6 || M == 9 || M == 11) {           MD = M + "/" + (D - i + 30);         } else {           if (((Y % 4) == 0) && ((Y % 100) != 0) || ((Y % 400) == 0)) {   //闰年             MD = M + "/" + (D - i + 29);           } else {             MD = M + "/" + (D - i + 28);           }         }         dateArr[i] = MD;       }     }     console.log(dateArr);   }   getPreMonthDate();      //结果是否你想要的呢?      /*[ '5/19',     '5/18',     '5/17',     '5/16',     '5/15',     '5/14',     '5/13',     '5/12',     '5/11',     '5/10',     '5/9',     '5/8',     '5/7',     '5/6',     '5/5',     '5/4',     '5/3',     '5/2',     '5/1',     '4/30',     '4/29',     '4/28',     '4/27',     '4/26',     '4/25',     '4/24',     '4/23',     '4/22',     '4/21',     '4/20',     '4/19' ] */  

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

本文固定链接: http://www.js-code.com/js/js_1466.html