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

摘要:
JS Get(Issue No.,current date,一周的第一天和最后一天,以及当前月份的第一天,最后一天)函数1/**2*获取当前月份的Issue No.3*返回格式:YYYY-mm4**/5functiongetCurrentMonthIssue(date){6letmonth=parseInt(date.getMonth()+1);7if(month˂10){8 month='0'+month9}10返回日期。获取完整年份

    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  --本月最后一天时间

免责声明:文章转载自《JS 获取(期号、当前日期、本周第一天、最后一天及当前月第一、最后天函数)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇UniAPP 利用sqlite保存数据ubuntu 安装ncurses下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

php 关于时区 date gmdate date_default_timezone_set/get 终极答疑

关于GMT和UTC时间? GMT+0800 Greenwich: [gri:nitf], 格林威治/格林尼治.是英国伦敦泰晤士河附近的一个小镇 Mean: adj. 自私的; n. 平均.. he is mean about money. GMT就是格林威治平均/标准时间 什么叫本初子午线? 国外叫 : prime mer'idian.只是在中国内才叫本初...

java日期格式大全 format SimpleDateFormat(转)

java日期格式大全 format SimpleDateFormat /*** 字符串转换为java.util.Date<br>* 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br>* yy/MM/dd HH:mm:ss 如 '2002/1/1...

Java8 时间API

Java8中,java.time主要包含下面几个类 Instant:时间戳 Duration:持续时间,时间差 LocalDate:只包含日期,比如:2016-10-20 LocalTime:只包含时间,比如:23:12:10 LocalDateTime:包含日期和时间,比如:2016-10-20 23:14:21 Period:时间段 ZoneOffse...

解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题

当我们需要将一串日期字符串转换为具体的Date格式的时候,往往需要用到new Date("xxxx")方法。 当时在IE浏览器下,会遇到这种问题: new Date('2016-01-01 00:00:00') //却返回这个值Invalid Date,转换失败 但是这个方法却在谷歌浏览器上可以返回正确的结果。 解决方式:最终发现是字符...

判断当前日期是否是当月数据

/** *获取当月的开始日期 *@author: lkf *@Date: 2017/9/13 11:16 */ public static Date getCurrentMonthStartTime() { SimpleDateFormat shortSdf = new SimpleDateF...

LambdaQueryWrapper 日期格式转换,匹配精确日期及为空日期

/** * 日期过滤 */ private void dateFilter(String dateField, LambdaQueryWrapper<TmpOrderAuxiliaryPlanEntity> wrapper, String fieldName) { List<String&g...