1. 程式人生 > 實用技巧 >Python之控制已開啟瀏覽器以及頁面點選和切換控制權

Python之控制已開啟瀏覽器以及頁面點選和切換控制權

  最近在寫的專案對日期獲取的方法種類用的比較多,於是在此進行錄入下來。如果一下程式碼有錯誤或者有bug還請路過的大神指點指點,這些方法對你們有幫助請給個支援,有其他的好方法也請留言哦。轉載時請加上本文連結。

 1 /**
 2  *  獲取幾天之前日期
 3  */
 4  daysAgo(dayNum = 0) {
 5     let myDate = new Date()
 6     let lw = new Date(myDate - 1000 * 60 * 60 * 24 * dayNum) // 最後一個數字多少天前的意思
 7     let lastY = lw.getFullYear()
8 let lastM = lw.getMonth() + 1 9 let lastD = lw.getDate() 10 let startdate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD) 11 return startdate 12 }

 1 /**
 2  *  獲取幾個月之前日期
 3  */
 4  monthAgo(monthNum = 0) {
 5     let newDate = new Date();
6 let year = newDate.getFullYear(); //獲取當前日期的年份 7 let month = newDate.getMonth() + 1; //獲取當前日期的月份 8 let day = newDate.getDate(); //獲取當前日期的日 9 let days = new Date(year, month, 0) 10 days = days.getDate() //獲取當前日期中月的天數 11 let year2 = year 12 13 // 進行月份計算 14 let month2 = parseInt(month) - monthNum
15 if (month2 <= 0) { 16 year2 = parseInt(year2) - parseInt(month2 / 12 == 0 ? 1 : Math.abs(parseInt(month2 / 12)) + 1) 17 month2 = 12 - (Math.abs(month2) % 12) 18 } 19 20 // 進行日期計算 21 let day2 = day 22 let days2 = new Date(year2, month2, 0) 23 days2 = days2.getDate() 24 if (day2 > days2) { 25 day2 = days2 26 } 27 28 let startdate = year2 + "-" + (month2 < 10 ? "0" + month2 : month2) + "-" + (day2 < 10 ? "0" + day2 : day2) 29 return startdate 30 }

 1 /**
 2  *  獲取幾年之前日期
 3  */
 4  yearAgo(yearNum = 0) {
 5     let newDate = new Date();
 6     let year = newDate.getFullYear(); //獲取當前日期的年份
 7     let month = newDate.getMonth() + 1; //獲取當前日期的月份
 8     let day = newDate.getDate(); //獲取當前日期的日
 9 
10     let year2 = year - yearNum;
11 
12     let startdate = year2 + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day)
13     return startdate
14 }

 1 /**
 2    *  根據日期獲取上一個月
 3    */
 4   preMonth(data) {
 5     let curMonth = new Date(data)
 6     let month = curMonth.getMonth() - 1;
 7     curMonth.setMonth(month);
 8     let newMonth = curMonth.getMonth() + 1;
 9     if (newMonth < 10) {
10       newMonth = "0" + newMonth;
11     }
12     return curMonth.getFullYear() + '-' + newMonth;
13   }

 1 /**
 2    *  獲取當前年月(yyyy-MM)
 3    */
 4   theCurrentMonth() {
 5     let date = new Date();
 6     let nowMonth = date.getMonth() + 1;
 7     if (nowMonth >= 1 && nowMonth <= 9) {
 8       nowMonth = "0" + nowMonth;
 9     }
10     let nowDate = date.getFullYear() + '-' + nowMonth;
11     return nowDate
12   }

 1 /**
 2    *  獲取上個月第一天
 3    */
 4   firstdate() {
 5     let date = new Date();
 6     let nowMonth = date.getMonth() + 1;
 7     if (nowMonth >= 1 && nowMonth <= 9) {
 8       nowMonth = "0" + nowMonth;
 9     }
10     let nowDate = date.getFullYear() + '-' + nowMonth + '-01';
11     return nowDate
12   }

 1 /**
 2    *  獲取上個月最後一天
 3    */
 4   enddate() {
 5     let date = new Date();
 6     let day = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
 7     let nowMonth = date.getMonth() + 1;
 8     if (nowMonth >= 1 && nowMonth <= 9) {
 9       nowMonth = "0" + nowMonth;
10     }
11     let nowDate = date.getFullYear() + '-' + nowMonth + '-' + day;
12 
13     return nowDate
14   }