mirror of
https://github.com/usememos/memos.git
synced 2025-12-17 22:28:52 +08:00
fix: dayjs - ISO 8601 compatible DATE_TIME_FORMAT (#4829)
This commit is contained in:
parent
0b1b471ee4
commit
8732575ea4
1 changed files with 5 additions and 3 deletions
|
|
@ -2,7 +2,8 @@ import dayjs from "dayjs";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
const DATE_TIME_FORMAT = "M/D/YYYY, H:mm:ss";
|
// must be compatible with JS Date.parse(), we use ISO 8601 (almost)
|
||||||
|
const DATE_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
|
||||||
|
|
||||||
// convert Date to datetime string.
|
// convert Date to datetime string.
|
||||||
const formatDate = (date: Date): string => {
|
const formatDate = (date: Date): string => {
|
||||||
|
|
@ -23,12 +24,13 @@ const DateTimeInput: React.FC<Props> = ({ value, onChange }) => {
|
||||||
onBlur={(e) => {
|
onBlur={(e) => {
|
||||||
const inputValue = e.target.value;
|
const inputValue = e.target.value;
|
||||||
if (inputValue) {
|
if (inputValue) {
|
||||||
const date = dayjs(inputValue, DATE_TIME_FORMAT, true).toDate();
|
// note: inputValue must be compatible with JS Date.parse()
|
||||||
|
const date = dayjs(inputValue).toDate();
|
||||||
// Check if the date is valid.
|
// Check if the date is valid.
|
||||||
if (!isNaN(date.getTime())) {
|
if (!isNaN(date.getTime())) {
|
||||||
onChange(date);
|
onChange(date);
|
||||||
} else {
|
} else {
|
||||||
toast.error("Invalid datetime format. Use format: 12/31/2023, 23:59:59");
|
toast.error("Invalid datetime format. Use format: 2023-12-31 23:59:59");
|
||||||
e.target.value = formatDate(value);
|
e.target.value = formatDate(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue