feat: 添加DatetimeRange的快捷方式

This commit is contained in:
dhb52 2024-10-24 17:18:52 +08:00
parent 7957e60319
commit a4f7b7c057
5 changed files with 28 additions and 2 deletions

View File

@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { $t } from '@/locales'; import { $t } from '@/locales';
import { monthRange } from '@/utils/common'; import { dayRange, monthRange } from '@/utils/common';
defineOptions({ defineOptions({
name: 'DatetimeRange' name: 'DatetimeRange'
@ -13,6 +13,9 @@ const modelValue = defineModel<[string, string] | null>('value');
const createShortcuts = () => { const createShortcuts = () => {
const shortcuts: any = {}; const shortcuts: any = {};
shortcuts[$t('common.today')] = dayRange(1);
shortcuts[$t('common.lastWeek')] = dayRange(7);
shortcuts[$t('common.lastMonth')] = monthRange(1, 'month');
shortcuts[$t('common.currentMonth')] = monthRange(0, 'month'); shortcuts[$t('common.currentMonth')] = monthRange(0, 'month');
shortcuts[$t('common.lastMonth')] = monthRange(1, 'month'); shortcuts[$t('common.lastMonth')] = monthRange(1, 'month');
shortcuts[$t('common.lastTwoMonth')] = monthRange(2, 'month'); shortcuts[$t('common.lastTwoMonth')] = monthRange(2, 'month');

View File

@ -210,6 +210,8 @@ const local: App.I18n.Schema = {
}, },
updateDt: 'Updated Time', updateDt: 'Updated Time',
createDt: 'Created Time', createDt: 'Created Time',
today: 'Today',
lastWeek: 'Last Week',
currentMonth: 'Current Month', currentMonth: 'Current Month',
lastMonth: 'Last Month', lastMonth: 'Last Month',
lastTwoMonth: 'Last 2 Month' lastTwoMonth: 'Last 2 Month'

View File

@ -210,6 +210,8 @@ const local: App.I18n.Schema = {
}, },
updateDt: '更新时间', updateDt: '更新时间',
createDt: '创建时间', createDt: '创建时间',
today: '今天',
lastWeek: '最近一周',
currentMonth: '当月', currentMonth: '当月',
lastMonth: '最近一月', lastMonth: '最近一月',
lastTwoMonth: '最近两月' lastTwoMonth: '最近两月'

View File

@ -491,6 +491,8 @@ declare namespace App {
}; };
updateDt: string; updateDt: string;
createDt: string; createDt: string;
today: string;
lastWeek: string;
currentMonth: string; currentMonth: string;
lastMonth: string; lastMonth: string;
lastTwoMonth: string; lastTwoMonth: string;

View File

@ -120,7 +120,7 @@ export function toggleHtmlClass(className: string) {
} }
/** /**
* `最近n个自然月` timestamp时间区间 * `最近n个自然月` timestamp
* *
* @param months * @param months
* @param startOf * @param startOf
@ -133,6 +133,23 @@ export function monthRange(months: number = 1, startOf: dayjs.OpUnitType = 'day'
]; ];
} }
/**
* `最近n个自然月` timestamp
*
* @param days
* @param startOf
* @returns timestamp时间区间
*/
export function dayRange(days: number = 1) {
return [
dayjs()
.subtract(days - 1, 'day')
.startOf('day')
.valueOf(),
dayjs().endOf('day').valueOf()
] as [number, number];
}
/** /**
* `最近n个自然月` * `最近n个自然月`
* *