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

Js代码 复制代码 收藏代码
  1. /**
  2. * 获取本周、本季度、本月、上月的开始日期、结束日期
  3. */
  4. var now = new Date(); //当前日期
  5. var nowDayOfWeek = now.getDay(); //今天本周的第几天
  6. var nowDay = now.getDate(); //当前日
  7. var nowMonth = now.getMonth(); //当前月
  8. var nowYear = now.getYear(); //当前年
  9. nowYear += (nowYear < 2000) ? 1900 : 0; //
  10. var lastMonthDate = new Date(); //上月日期
  11. lastMonthDate.setDate(1);
  12. lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
  13. var lastYear = lastMonthDate.getYear();
  14. var lastMonth = lastMonthDate.getMonth();
  15. //格式化日期:yyyy-MM-dd
  16. function formatDate(date) {
  17. var myyear = date.getFullYear();
  18. var mymonth = date.getMonth()+1;
  19. var myweekday = date.getDate();
  20. if(mymonth < 10){
  21. mymonth = "0" + mymonth;
  22. }
  23. if(myweekday < 10){
  24. myweekday = "0" + myweekday;
  25. }
  26. return (myyear+"-"+mymonth + "-" + myweekday);
  27. }
  28. //获得某月的天数
  29. function getMonthDays(myMonth){
  30. var monthStartDate = new Date(nowYear, myMonth, 1);
  31. var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  32. var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
  33. return days;
  34. }
  35. //获得本季度的开始月份
  36. function getQuarterStartMonth(){
  37. var quarterStartMonth = 0;
  38. if(nowMonth<3){
  39. quarterStartMonth = 0;
  40. }
  41. if(2<nowMonth && nowMonth<6){
  42. quarterStartMonth = 3;
  43. }
  44. if(5<nowMonth && nowMonth<9){
  45. quarterStartMonth = 6;
  46. }
  47. if(nowMonth>8){
  48. quarterStartMonth = 9;
  49. }
  50. return quarterStartMonth;
  51. }
  52. //获得本周的开始日期
  53. function getWeekStartDate() {
  54. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  55. return formatDate(weekStartDate);
  56. }
  57. //获得本周的结束日期
  58. function getWeekEndDate() {
  59. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  60. return formatDate(weekEndDate);
  61. }
  62. //获得本月的开始日期
  63. function getMonthStartDate(){
  64. var monthStartDate = new Date(nowYear, nowMonth, 1);
  65. return formatDate(monthStartDate);
  66. }
  67. //获得本月的结束日期
  68. function getMonthEndDate(){
  69. var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  70. return formatDate(monthEndDate);
  71. }
  72. //获得上月开始时间
  73. function getLastMonthStartDate(){
  74. var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  75. return formatDate(lastMonthStartDate);
  76. }
  77. //获得上月结束时间
  78. function getLastMonthEndDate(){
  79. var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  80. return formatDate(lastMonthEndDate);
  81. }
  82. //获得本季度的开始日期
  83. function getQuarterStartDate(){
  84. var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  85. return formatDate(quarterStartDate);
  86. }
  87. //或的本季度的结束日期
  88. function getQuarterEndDate(){
  89. var quarterEndMonth = getQuarterStartMonth() + 2;
  90. var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
  91. return formatDate(quarterStartDate);
  92. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值