JS 获取(期号、当前日期、本周第一天、最后一天及当前月第一、最后天函数)

    JS 获取(期号、当前日期、本周第一天、最后一天及当前月第一、最后天函数

  1 /**
  2      * 获取当前月期号
  3      * 返回格式: YYYY-mm
  4      * */
  5     function getCurrentMonthIssue(date) {
  6         let month = parseInt(date.getMonth() + 1);
  7         if (month < 10) {
  8             month = '0' + month
  9         }
 10         return date.getFullYear() + "-" + month;
 11     }
 12 
 13     /**
 14      * 获取当前的日期
 15      * 返回格式: YYYY-mm-dd
 16      * */
 17     function getCurrentDate(date) {
 18         let month = parseInt(date.getMonth() + 1);
 19         let day = date.getDate();
 20         if (month < 10) {
 21             month = '0' + month
 22         }
 23         if (day < 10) {
 24             day = '0' + day
 25         }
 26         return date.getFullYear() + '-' + month + '-' + day;
 27     }
 28 
 29     /**
 30      * 获取本周的第一天
 31      * 返回格式: YYYY-mm-dd
 32      *    例子: 当日为: 2020-11-27
 33      *      返回日期为: 2020-11-23
 34      * */
 35     function getCurrentWeekFirstDay(date) {
 36         let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
 37         let firstMonth = Number(weekFirstDay.getMonth()) + 1
 38 
 39         if (firstMonth < 10) {
 40             firstMonth = '0' + firstMonth
 41         }
 42         let weekFirstDays = weekFirstDay.getDate();
 43         if (weekFirstDays < 10) {
 44             weekFirstDays = '0' + weekFirstDays;
 45         }
 46         return weekFirstDay.getFullYear() + '-' + firstMonth + '-' + weekFirstDays;
 47     }
 48 
 49     /**
 50      * 获取本周的最后一天
 51      * 返回格式: YYYY-mm-dd
 52      *    例子: 当日为: 2020-11-27
 53      *      返回日期为: 2020-11-29
 54      * */
 55     function getCurrentWeekLastDay(date) {
 56         let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
 57         let weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)
 58         let lastMonth = Number(weekLastDay.getMonth()) + 1
 59         if (lastMonth < 10) {
 60             lastMonth = '0' + lastMonth
 61         }
 62         let weekLastDays = weekLastDay.getDate();
 63         if (weekLastDays < 10) {
 64             weekLastDays = '0' + weekLastDays;
 65         }
 66         return weekFirstDay.getFullYear() + '-' + lastMonth + '-' + weekLastDays;
 67     }
 68 
 69     /**
 70      * 获取当前月的第一天
 71      * 返回格式: YYYY-mm-dd
 72      *    例子: 当日为: 2020-11-27
 73      *      返回日期为: 2020-11-01
 74      * */
 75     function getCurrentMonthFirstDay() {
 76         let date = new Date();
 77         date.setDate(1);
 78         let month = parseInt(date.getMonth() + 1);
 79         let day = date.getDate();
 80         if (month < 10) {
 81             month = '0' + month
 82         }
 83         if (day < 10) {
 84             day = '0' + day
 85         }
 86         return date.getFullYear() + '-' + month + '-' + day;
 87     }
 88 
 89     /**
 90      * 获取当前月的最后一天
 91      * 返回格式: YYYY-mm-dd
 92      *    例子: 当日为: 2020-11-27
 93      *      返回日期为: 2020-11-30
 94      * */
 95     function getCurrentMonthLastDay() {
 96         let date = new Date();
 97         let currentMonth = date.getMonth();
 98         let nextMonth = ++currentMonth;
 99         let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
100         let oneDay = 1000 * 60 * 60 * 24;
101         let lastTime = new Date(nextMonthFirstDay - oneDay);
102         let month = parseInt(lastTime.getMonth() + 1);
103         let day = lastTime.getDate();
104         if (month < 10) {
105             month = '0' + month
106         }
107         if (day < 10) {
108             day = '0' + day
109         }
110         return date.getFullYear() + '-' + month + '-' + day;
111     }

使用方式:

1 let date = new Date();
2 // 例当日时间是 2020-11-27 
3 getCurrentMonthIssue(date);         // result: 2020-11  --期号
4 getCurrentDate(date);               // result: 2020-11-27  --当前日期
5 getCurrentWeekFirstDay(date);       // result: 2020-11-23  --本周第一天时间
6 getCurrentWeekLastDay(date);        // result: 2020-11-29  --本周最后一天时间
7 getCurrentMonthFirstDay(date);      // result: 2020-11-01  --本月第一天时间
8 getCurrentMonthLastDay(date);       // result: 2020-11-30  --本月最后一天时间
原文地址:https://www.cnblogs.com/jhtian/p/14050217.html