123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import { Injectable } from "@angular/core";
- import {
- addDays,
- startOfWeek,
- endOfMonth,
- startOfDay,
- addMonths,
- startOfMonth,
- startOfYear,
- addYears,
- endOfYear,
- endOfWeek,
- endOfDay,
- format,
- } from "date-fns";
- @Injectable({
- providedIn: "root",
- })
- export class DateService {
- constructor() {}
- // 获取日期快捷类型
- getDateType(dateRange){
- console.log('dateRange:', dateRange)
- let [startDate, endDate] = dateRange;
- if(startDate == format(this.date().yesterdayStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().yesterdayEndDate, 'yyyy-MM-dd HH:mm:ss')){
- return 1;
- }else if(startDate == format(this.date().thisWeekStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().thisWeekEndDate, 'yyyy-MM-dd HH:mm:ss')){
- return 2;
- }else if(startDate == format(this.date().thisMonthStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().thisMonthEndDate, 'yyyy-MM-dd HH:mm:ss')){
- return 3;
- }else if(startDate == format(this.date().thisYearStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().thisYearEndDate, 'yyyy-MM-dd HH:mm:ss')){
- return 4;
- }else if(startDate == format(this.date().lastWeekStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().lastWeekEndDate, 'yyyy-MM-dd HH:mm:ss')){
- return 5;
- }else if(startDate == format(this.date().lastMonthStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().lastMonthEndDate, 'yyyy-MM-dd HH:mm:ss')){
- return 6;
- }else if(startDate == format(this.date().lastYearStartDate, 'yyyy-MM-dd HH:mm:ss') && endDate == format(this.date().lastYearEndDate, 'yyyy-MM-dd HH:mm:ss')){
- return 7;
- }
- }
- /**
- * 日期操作
- */
- date() {
- //获得昨日的开始日期(昨日 00:00:00)
- function getYesterdayStartDate() {
- return addDays(startOfDay(new Date()), -1);
- }
- //获得昨日的结束日期(昨日 23:59:59)
- function getYesterdayEndDate() {
- return addDays(endOfDay(new Date()), -1);
- }
- //获得本周的开始日期(本周一 00:00:00)
- function getThisWeekStartDate() {
- return startOfWeek(new Date(), { weekStartsOn: 1 });
- }
- //获得本周的结束日期(本周日 23:59:59)
- function getThisWeekEndDate() {
- return endOfWeek(new Date(), { weekStartsOn: 1 });
- }
- //获得本月开始时间(本月1号 00:00:00)
- function getThisMonthStartDate() {
- return startOfMonth(new Date());
- }
- //获得本月结束时间(本月底 23:59:59)
- function getThisMonthEndDate() {
- return endOfMonth(new Date());
- }
- //获得本年的开始日期(本年1月1日 00:00:00)
- function getThisYearStartDate() {
- return startOfYear(new Date());
- }
- //获得本年的结束日期(本年12月31日 23:59:59)
- function getThisYearEndDate() {
- return endOfYear(new Date());
- }
- //获得上周的开始日期(上周一 00:00:00)
- function getLastWeekStartDate() {
- return addDays(startOfWeek(new Date()), -6);
- }
- //获得上周的结束日期(上周日 23:59:59)
- function getLastWeekEndDate() {
- return addDays(endOfWeek(new Date()), -6);
- }
- //获得上月开始时间(上月1号 00:00:00)
- function getLastMonthStartDate() {
- return startOfMonth(addMonths(new Date(), -1));
- }
- //获得上月结束时间(上月底 23:59:59)
- function getLastMonthEndDate() {
- return endOfMonth(addMonths(new Date(), -1));
- }
- //获得上年的开始日期(去年1月1日 00:00:00)
- function getLastYearStartDate() {
- return startOfYear(addYears(new Date(), -1));
- }
- //获得上年的结束日期(去年12月31日 23:59:59)
- function getLastYearEndDate() {
- return endOfYear(addYears(new Date(), -1));
- }
- return {
- yesterdayStartDate: getYesterdayStartDate(),
- yesterdayEndDate: getYesterdayEndDate(),
- thisWeekStartDate: getThisWeekStartDate(),
- thisWeekEndDate: getThisWeekEndDate(),
- thisMonthStartDate: getThisMonthStartDate(),
- thisMonthEndDate: getThisMonthEndDate(),
- thisYearStartDate: getThisYearStartDate(),
- thisYearEndDate: getThisYearEndDate(),
- lastWeekStartDate: getLastWeekStartDate(),
- lastWeekEndDate: getLastWeekEndDate(),
- lastMonthStartDate: getLastMonthStartDate(),
- lastMonthEndDate: getLastMonthEndDate(),
- lastYearStartDate: getLastYearStartDate(),
- lastYearEndDate: getLastYearEndDate(),
- };
- }
- }
|