12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { Injectable } from "@angular/core";
- import {
- addDays,
- startOfWeek,
- endOfMonth,
- startOfDay,
- addMonths,
- startOfMonth,
- startOfYear,
- addYears,
- endOfYear,
- endOfWeek,
- } from "date-fns";
- @Injectable({
- providedIn: "root",
- })
- export class DateService {
- constructor() {}
- /**
- * 日期操作
- */
- 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 {
- lastWeekStartDate: getLastWeekStartDate(),
- lastWeekEndDate: getLastWeekEndDate(),
- lastMonthStartDate: getLastMonthStartDate(),
- lastMonthEndDate: getLastMonthEndDate(),
- lastYearStartDate: getLastYearStartDate(),
- lastYearEndDate: getLastYearEndDate(),
- };
- }
- }
|