From a975be99e9bb5a553ad5c4e777d9d9477b7ccda1 Mon Sep 17 00:00:00 2001 From: honzapatCZ Date: Wed, 3 Jun 2026 15:15:08 +0200 Subject: [PATCH] use dayjs for dates --- Form/Core/DateFields.js | 13 +++++-------- Form/Core/DateTimeField.js | 22 ++++++---------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/Form/Core/DateFields.js b/Form/Core/DateFields.js index 8e88407..693d660 100644 --- a/Form/Core/DateFields.js +++ b/Form/Core/DateFields.js @@ -1,5 +1,6 @@ import tw, { styled } from "twin.macro"; import "styled-components/macro"; +import dayjs from "dayjs"; import Input, { useField } from "@shared/nej-react-components/Parts/Input"; @@ -7,16 +8,12 @@ export default function DateField(props) { const [{onChange, value, ...field}, meta, helpers] = useField(props); - let realDate = (typeof(value) === "string" ? new Date(Date.parse(value)) : value); - if(typeof(realDate) != "object") - realDate = null; - //check if its ivnalid, if so make it null - if(isNaN(realDate)) - realDate = null; + const parsedDate = dayjs(value); + const isValid = value && parsedDate.isValid(); const {value: _x, onChange: _y, ...restProps} = props; return onChange(new Date(e.target.value))} {...field} {...restProps} />; + value={isValid ? parsedDate.format("YYYY-MM-DD") : ""} + onChange={(e)=>onChange(e.target.value ? dayjs(e.target.value).toDate() : null)} {...field} {...restProps} />; } diff --git a/Form/Core/DateTimeField.js b/Form/Core/DateTimeField.js index bd398ba..c7de302 100644 --- a/Form/Core/DateTimeField.js +++ b/Form/Core/DateTimeField.js @@ -1,5 +1,6 @@ import tw, { styled } from "twin.macro"; import "styled-components/macro"; +import dayjs from "dayjs"; import Input, { useField } from "@shared/nej-react-components/Parts/Input"; @@ -7,26 +8,15 @@ export default function DateTimeField(props) { const [{onChange, value, ...field}, meta, helpers] = useField(props); - //console.log(value) - /*** - * @type {Date} - */ - let realDate = (typeof(value) === "string" ? new Date(Date.parse( (value.length > 19 || value.length < 10) ? value : value + "Z")) : value); - if(typeof(realDate) != "object") - realDate = null; - //check if its ivnalid, if so make it null - if(isNaN(realDate)) - realDate = null; + const parsedDate = dayjs(value); + const isValid = value && parsedDate.isValid(); const {value: _x, onChange: _y, ...restProps} = props; - // console.log(realDate) - // console.log(realDate?.toISOString().slice(0,19)) return { - const value = e.target.value; - console.log( (value.length > 19 || value.length < 10) ? value : value + "Z") - onChange(new Date( (value.length > 19 || value.length < 10) ? value : value + "Z")) + const val = e.target.value; + onChange(val ? dayjs(val).toDate() : null); }} {...field} {...restProps} />; }