diff --git a/src/utils/index.d.ts b/src/utils/index.d.ts index b3ad4fe..df86bd1 100644 --- a/src/utils/index.d.ts +++ b/src/utils/index.d.ts @@ -350,12 +350,19 @@ export function validatorEndTime(options: { startTime: string; message?: string; }; /** - * 验证时间是否大于等于当前时间 + * 验证时间是否大于当前时间 */ export function validatorTimeGTCurrentDay(options?: { message?: string; type?: "date" | "datetime" }): { validator: (_: any, value: any) => Promise; }; +/** + * 验证时间是否大于等于当前时间 + */ +export function validatorTimeGECurrentDay(options?: { message?: string; type?: "date" | "datetime" }): { + validator: (_: any, value: any) => Promise; +}; + /** * 动态加载js资源 */ diff --git a/src/utils/index.js b/src/utils/index.js index 2868d97..e23968d 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -487,7 +487,7 @@ export function processTreeDataForOnlyLastLevel(options) { * 验证结束时间是否大于开始时间 */ export function validatorEndTime(options) { - let { startTime, message = "结束时间不能早于开始时间", type = "" } = options; + let { startTime, message = "结束时间不能早于开始时间", type = "" } = options || {}; return { validator: (_, value) => { @@ -514,17 +514,45 @@ export function validatorEndTime(options) { } /** - * 验证时间是否大于等于当前时间 + * 验证时间是否大于当前时间 */ export function validatorTimeGTCurrentDay(options) { - const { message = "需要大于当前时间", type = "date" } = options; + const { message = "需要大于当前时间", type = "date" } = options || {}; return { validator: (_, value) => { if (!value) return Promise.resolve(); const selectedDate = dayjs.isDayjs(value) ? value : dayjs(value); - if (selectedDate.isBefore(dayjs(), type === "date" ? "day" : "seconds")) + const now = dayjs(); + const isAfter = type === "date" + ? selectedDate.isAfter(now, "day") + : selectedDate.isAfter(now, "seconds"); + + if (!isAfter) + return Promise.reject(message); + else + return Promise.resolve(); + }, + }; +} +/** + * 验证时间是否大于等于当前时间 + */ +export function validatorTimeGECurrentDay(options) { + const { message = "需要大于等于当前时间", type = "date" } = options || {}; + return { + validator: (_, value) => { + if (!value) + return Promise.resolve(); + + const selectedDate = dayjs.isDayjs(value) ? value : dayjs(value); + const now = dayjs(); + const isBefore = type === "date" + ? selectedDate.isBefore(now, "day") + : selectedDate.isBefore(now, "seconds"); + + if (isBefore) return Promise.reject(message); else return Promise.resolve();