date.service.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Injectable } from "@angular/core";
  2. import {
  3. addDays,
  4. startOfWeek,
  5. endOfMonth,
  6. startOfDay,
  7. addMonths,
  8. startOfMonth,
  9. startOfYear,
  10. addYears,
  11. endOfYear,
  12. endOfWeek,
  13. endOfDay,
  14. format,
  15. } from "date-fns";
  16. @Injectable({
  17. providedIn: "root",
  18. })
  19. export class DateService {
  20. constructor() {}
  21. // 获取日期快捷类型
  22. getDateType(dateRange){
  23. console.log('dateRange:', dateRange)
  24. let [startDate, endDate] = dateRange;
  25. if(startDate == format(this.date().yesterdayStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().yesterdayEndDate, 'yyyy-MM-dd HH:mm:ss')){
  26. return 1;
  27. }else if(startDate == format(this.date().thisWeekStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().thisWeekEndDate, 'yyyy-MM-dd HH:mm:ss')){
  28. return 2;
  29. }else if(startDate == format(this.date().thisMonthStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().thisMonthEndDate, 'yyyy-MM-dd HH:mm:ss')){
  30. return 3;
  31. }else if(startDate == format(this.date().thisYearStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().thisYearEndDate, 'yyyy-MM-dd HH:mm:ss')){
  32. return 4;
  33. }else if(startDate == format(this.date().lastWeekStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().lastWeekEndDate, 'yyyy-MM-dd HH:mm:ss')){
  34. return 5;
  35. }else if(startDate == format(this.date().lastMonthStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().lastMonthEndDate, 'yyyy-MM-dd HH:mm:ss')){
  36. return 6;
  37. }else if(startDate == format(this.date().lastYearStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().lastYearEndDate, 'yyyy-MM-dd HH:mm:ss')){
  38. return 7;
  39. }
  40. }
  41. /**
  42. * 日期操作
  43. */
  44. date() {
  45. //获得昨日的开始日期(昨日 00:00:00)
  46. function getYesterdayStartDate() {
  47. return addDays(startOfDay(new Date()), -1);
  48. }
  49. //获得昨日的结束日期(昨日 23:59:59)
  50. function getYesterdayEndDate() {
  51. return addDays(endOfDay(new Date()), -1);
  52. }
  53. //获得本周的开始日期(本周一 00:00:00)
  54. function getThisWeekStartDate() {
  55. return startOfWeek(new Date(), { weekStartsOn: 1 });
  56. }
  57. //获得本周的结束日期(本周日 23:59:59)
  58. function getThisWeekEndDate() {
  59. return endOfWeek(new Date(), { weekStartsOn: 1 });
  60. }
  61. //获得本月开始时间(本月1号 00:00:00)
  62. function getThisMonthStartDate() {
  63. return startOfMonth(new Date());
  64. }
  65. //获得本月结束时间(本月底 23:59:59)
  66. function getThisMonthEndDate() {
  67. return endOfMonth(new Date());
  68. }
  69. //获得本年的开始日期(本年1月1日 00:00:00)
  70. function getThisYearStartDate() {
  71. return startOfYear(new Date());
  72. }
  73. //获得本年的结束日期(本年12月31日 23:59:59)
  74. function getThisYearEndDate() {
  75. return endOfYear(new Date());
  76. }
  77. //获得上周的开始日期(上周一 00:00:00)
  78. function getLastWeekStartDate() {
  79. return addDays(startOfWeek(new Date()), -6);
  80. }
  81. //获得上周的结束日期(上周日 23:59:59)
  82. function getLastWeekEndDate() {
  83. return addDays(endOfWeek(new Date()), -6);
  84. }
  85. //获得上月开始时间(上月1号 00:00:00)
  86. function getLastMonthStartDate() {
  87. return startOfMonth(addMonths(new Date(), -1));
  88. }
  89. //获得上月结束时间(上月底 23:59:59)
  90. function getLastMonthEndDate() {
  91. return endOfMonth(addMonths(new Date(), -1));
  92. }
  93. //获得上年的开始日期(去年1月1日 00:00:00)
  94. function getLastYearStartDate() {
  95. return startOfYear(addYears(new Date(), -1));
  96. }
  97. //获得上年的结束日期(去年12月31日 23:59:59)
  98. function getLastYearEndDate() {
  99. return endOfYear(addYears(new Date(), -1));
  100. }
  101. return {
  102. yesterdayStartDate: getYesterdayStartDate(),
  103. yesterdayEndDate: getYesterdayEndDate(),
  104. thisWeekStartDate: getThisWeekStartDate(),
  105. thisWeekEndDate: getThisWeekEndDate(),
  106. thisMonthStartDate: getThisMonthStartDate(),
  107. thisMonthEndDate: getThisMonthEndDate(),
  108. thisYearStartDate: getThisYearStartDate(),
  109. thisYearEndDate: getThisYearEndDate(),
  110. lastWeekStartDate: getLastWeekStartDate(),
  111. lastWeekEndDate: getLastWeekEndDate(),
  112. lastMonthStartDate: getLastMonthStartDate(),
  113. lastMonthEndDate: getLastMonthEndDate(),
  114. lastYearStartDate: getLastYearStartDate(),
  115. lastYearEndDate: getLastYearEndDate(),
  116. };
  117. }
  118. }