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

/**

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 获取(期号、当前日期、本周第一天、最后一天及当前月第一、最后天函数)    的更多相关文章

  1. 现代浏览器原生js获取id号方法

    <div id="tests" class="a b c" style="color:#f00">123</div> ...

  2. js获取带#号链接后的参数

    现在许多的主流网站都将'#'大规模用于重要URL中,我们通过正则表达式和window.location.search获取参数已经行不通了. 一.'#'号是什么 1.#代表网页中的一个位置.其后面的字符 ...

  3. ReportingService 通过RowNumber函数获取行号和生成隔行变色样式

    以前一直没有搞明白SSRS里面的RowNumber函数到底该怎么用,所以一直没有很好的办法在SSRS中的表格上实现隔行变色的样式,实现隔行变色的关键就是获取表格中每一行的行号.在最近了解了下这个函数, ...

  4. LODOP打印用JS获取的当前日期

    该文详细一步步解释JS获取当前时间的方法,新手小白也看到懂,最后是实际的获取当前年月份的方法.JS中的Date()对象,包含很多当前系统时间的方法,首先建立一个Date()对象,这里取名为date,然 ...

  5. js 获取 本周、上周、本月、上月、本季度、上季度的开始结束日期

    js 获取 本周.上周.本月.上月.本季度.上季度的开始结束日期 /**  * 获取本周.本季度.本月.上月的开始日期.结束日期  */ var now = new Date(); //当前日期 va ...

  6. js 获取当前月份 第一天和最后一天

    js 获取当前月份 第一天和最后一天 var now = new Date(); //当前日期 var nowMonth = now.getMonth(); //当前月 var nowYear = n ...

  7. js获取本周日期

    JS获取到本周的日期 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  8. js获取当前日期是一年中的第几天

    js获取当前日期为一年中的第几天 const currentYear = new Date().getFullYear().toString(); // 今天减今年的第一天(xxxx年01月01日) ...

  9. JS获取当前日期及 js获取当前时间和一星期前的时间

    var myDate = new Date();     new Date() 代表当前 年 月 日 时 分 秒: myDate.getYear();        //获取当前年份(2位),getY ...

随机推荐

  1. crash安装使用

     cash作为Linux内核调试的工具是必不可少少的一部分,但是他的下载并不是 yum install一下这么简单的,本文就来讲一下如何安装crash进行调试.  首先就是了解Linux的内核版本.这 ...

  2. day2(APlview+Serializers使用)

    1.APIview使用 ModelVIewSet 是对 APIView 封装  ModelSerializer是对Serializeer 1.1 在user/urls.py中添加路由 urlpatte ...

  3. Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?

    Python中splitlines([keepends])方法用于返回由原字符串中各行组成的列表,在行边界的位置拆分. 如果keepends=True,结果列表中包含行边界,否则不包含 行边界的字符. ...

  4. PyQt(Python+Qt)学习随笔:QColumnView的resizeGripsVisible属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QColumnView在一个视图中展示多个列表,每个下级列表是上一个列表的数据项的分支, QColu ...

  5. tensorflow 指定版本安装

    首先,建议在anaconda中创建虚拟环境,教程已写,参考上一篇 下载之前建议设置pip清华源(用以提速,可百度) 设置下载源 pip config set global.index-url http ...

  6. IDM 6.37.8 绿色特别版 (4月4日更新)

    Internet Download Manager,全球最流行的下载工具.Internet Download Manager (简称IDM) Windows 平台功能强大的多线程下载工具,国外非常受欢 ...

  7. 题解-NOI2016 优秀的拆分

    NOI2016 优秀的拆分 \(T\) 组测试数据.求字符串 \(s\) 的所有子串拆成 \(AABB\) 形式的方案总和. 数据范围:\(1\le T\le 10\),\(1\le n\le 3\c ...

  8. 解压版mysql+免破解版Navicat,好用!

    解压版mysql安装流程 获取mysql压缩包 获取地址: 链接:https://pan.baidu.com/s/1HqdFDQn_6ccPM0gOftApIg 提取码:n19t 获取压缩包后可安装压 ...

  9. js-enter提交表单导致页面刷新问题

    问题:当页面只有一个文本框时,使用键盘enter操作执行提交表单的时候,会导致页面进行刷新,并且参数也会自动添加到url中. 解决办法: 1.给form添加onsubmit=return false; ...

  10. Navicat操作Oracle

    1.出现connection to server failed, probable Oracle Net admin error 更换oci.dll,Navicat有自己默认的oci.dll,更换成你 ...