safety-eval-service-frontend/src/utils/dateFormat.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2026-06-23 18:07:30 +08:00
import dayjs from "dayjs";
/** 展示2026-01-23 11:39:09 */
export function formatDateTime(value) {
if (value == null || value === "") {
return "-";
}
const d = dayjs(value);
return d.isValid() ? d.format("YYYY-MM-DD HH:mm:ss") : String(value);
}
/** 展示2026-01-23 */
export function formatDate(value) {
if (value == null || value === "") {
return "-";
}
const d = dayjs(value);
return d.isValid() ? d.format("YYYY-MM-DD") : String(value);
}
/** 提交 API日期 */
export function toApiDate(value) {
if (value == null || value === "") {
return undefined;
}
if (typeof value === "string") {
return value.length > 10 ? value.slice(0, 10) : value;
}
const d = dayjs(value);
return d.isValid() ? d.format("YYYY-MM-DD") : undefined;
}
/** 提交 API日期时间 */
export function toApiDateTime(value) {
if (value == null || value === "") {
return undefined;
}
if (typeof value === "string") {
return value;
}
const d = dayjs(value);
return d.isValid() ? d.format("YYYY-MM-DDTHH:mm:ss") : undefined;
}
/** DatePicker 回显 */
export function toDayjs(value) {
if (value == null || value === "") {
return undefined;
}
const d = dayjs(value);
return d.isValid() ? d : undefined;
}