Skip to content

日期工具 ​

@axewo/fin-core 的日期工具分为两类:

  1. 会计期间工具(原生 JS)— 针对财务系统中 YYYY-MM 格式的期间操作
  2. 通用日期工具(dayjs)— 格式化、计算、比较等通用场景

会计期间工具 ​

getPeriod(date?) ​

获取会计期间字符串(YYYY-MM 格式)。

ts
import { getPeriod } from '@axewo/fin-core'

getPeriod('2024-05-15') // '2024-05'
getPeriod()             // 当前月份,如 '2024-06'

getFiscalYear(date?) ​

获取会计年度。

ts
import { getFiscalYear } from '@axewo/fin-core'

getFiscalYear('2024-05-15') // 2024

getPeriodRange(period) ​

获取期间的起止日期。

ts
import { getPeriodRange } from '@axewo/fin-core'

getPeriodRange('2024-02')
// { start: '2024-02-01', end: '2024-02-29' }

getPrevPeriod(period) / getNextPeriod(period) ​

获取上/下一个会计期间。

ts
import { getPrevPeriod, getNextPeriod } from '@axewo/fin-core'

getPrevPeriod('2024-01') // '2023-12'
getNextPeriod('2024-12') // '2025-01'

getPeriodList(start, end) ​

生成会计期间列表。

ts
import { getPeriodList } from '@axewo/fin-core'

getPeriodList('2024-01', '2024-03')
// ['2024-01', '2024-02', '2024-03']

getCurrentYearPeriods(year?) ​

获取当前年度所有会计期间(1-12月)。

ts
import { getCurrentYearPeriods } from '@axewo/fin-core'

getCurrentYearPeriods(2024)
// ['2024-01', '2024-02', ..., '2024-12']

formatDate(date) ​

格式化日期为 YYYY-MM-DD。

ts
import { formatDate } from '@axewo/fin-core'

formatDate(new Date(2024, 0, 15)) // '2024-01-15'

通用日期工具(dayjs) ​

formatDateTime(date, fmt?) ​

格式化日期时间。

ts
import { formatDateTime } from '@axewo/fin-core'

formatDateTime(new Date(), 'YYYY-MM-DD HH:mm:ss') // '2024-06-15 14:30:00'
formatDateTime('2024-01-01', 'MM/DD/YYYY')        // '01/01/2024'

formatDateShort(date, fmt?) ​

格式化日期(仅日期部分)。

ts
import { formatDateShort } from '@axewo/fin-core'

formatDateShort('2024-01-15') // '2024-01-15'

DATE_FORMATS ​

常用格式常量。

ts
import { DATE_FORMATS } from '@axewo/fin-core'

DATE_FORMATS.DATETIME      // 'YYYY-MM-DD HH:mm:ss'
DATE_FORMATS.DATE          // 'YYYY-MM-DD'
DATE_FORMATS.MONTH         // 'YYYY-MM'
DATE_FORMATS.TIME          // 'HH:mm:ss'
DATE_FORMATS.DATE_CN       // 'YYYY年MM月DD日'

addDays(date, days) / subDays(date, days) ​

增减天数。

ts
import { addDays, subDays } from '@axewo/fin-core'

addDays('2024-01-01', 7)  // Date 对象:2024-01-08
subDays('2024-01-15', 7)  // Date 对象:2024-01-08

addMonths(date, months) ​

增减月数。

ts
import { addMonths } from '@axewo/fin-core'

addMonths('2024-01-15', 3) // Date 对象:2024-04-15

diffDays(a, b) / diffMonths(a, b) ​

计算日期差。

ts
import { diffDays, diffMonths } from '@axewo/fin-core'

diffDays('2024-01-15', '2024-01-01')   // 14
diffMonths('2024-06-01', '2024-01-01') // 5

isBetween(date, start, end) ​

判断日期是否在范围内。

ts
import { isBetween } from '@axewo/fin-core'

isBetween('2024-06-15', '2024-01-01', '2024-12-31') // true

startOfDay(date) / endOfDay(date) ​

获取日始终止时刻。

ts
import { startOfDay, endOfDay } from '@axewo/fin-core'

startOfDay('2024-01-15') // 2024-01-15 00:00:00.000
endOfDay('2024-01-15')   // 2024-01-15 23:59:59.999

endOfMonth(date) ​

获取月末最后一天。

ts
import { endOfMonth } from '@axewo/fin-core'

endOfMonth('2024-02-01') // 2024-02-29

DatePicker 快捷选项 ​

dateShortcuts ​

日期范围快捷选项(单例,可直接用于 el-date-picker 的 shortcuts 属性)。

vue
<template>
  <el-date-picker
    v-model="dateRange"
    type="daterange"
    :shortcuts="dateShortcuts"
    placeholder="选择日期范围"
  />
</template>

<script setup>
import { ref } from 'vue'
import { dateShortcuts } from '@axewo/fin-core'

const dateRange = ref([])
</script>

包含的快捷选项:今天、昨天、本周、上周、本月、上月、本季度、上季度、本年、上年、最近 7 天、最近 30 天、最近 90 天。

singleDateShortcuts ​

单日快捷选项,用于选择单个日期。

ts
import { singleDateShortcuts } from '@axewo/fin-core'

toPeriod(d) / fromPeriod(period) ​

dayjs 与会计期间的桥接函数。

ts
import { toPeriod, fromPeriod } from '@axewo/fin-core'
import dayjs from 'dayjs'

toPeriod(dayjs('2024-05-15'))     // '2024-05'
fromPeriod('2024-05').toDate()    // Date 对象:2024-05-01