@@ -14,7 +14,7 @@ type DateConverterForm = {
1414export const useUnixTimeConverter = ( ) => {
1515 const methods = useCustomForm < DateConverterForm > ( {
1616 defaultValues : {
17- inputUnixTime : undefined ,
17+ inputUnixTime : '' ,
1818 timezone : undefined ,
1919 customFormat : '' ,
2020 } ,
@@ -30,48 +30,41 @@ export const useUnixTimeConverter = () => {
3030 ( ) => TIME_ZONES . map ( ( { label, tzCode } ) => ( { label : label , value : tzCode } ) ) ,
3131 [ ] ,
3232 ) ;
33- const timezone = watch ( 'timezone' ) ?? 'UTC' ;
34- const [ output , setOutput ] = useState < any > ( ( ) => convert ( getValues ( 'inputUnixTime' ) , timezone ) ) ;
33+ const [ output , setOutput ] = useState < ReturnType < typeof convert > > ( ) ;
3534
3635 const onChangeInputUnixTime = useCallback (
3736 ( value : DateConverterForm [ 'inputUnixTime' ] ) => {
3837 const d = dayjs . unix ( Number ( value ) ) ;
39- const { timezone, customFormat } = getValues ( ) ;
4038 if ( ! d . isValid ( ) ) return ;
4139
4240 setValue ( 'inputUnixTime' , value ) ;
43-
44- setOutput ( {
45- ...convert ( d , timezone ) ,
46- customFormat : customConvert ( d , timezone , customFormat ) ,
47- } ) ;
4841 } ,
49- [ setValue , getValues ] ,
42+ [ setValue ] ,
5043 ) ;
5144
5245 const onChangeTimezone = useCallback (
5346 ( event : string ) => {
54- console . log ( event ) ;
5547 setValue ( 'timezone' , event . toString ( ) ) ;
56- const { inputUnixTime } = getValues ( ) ;
57- onChangeInputUnixTime ( inputUnixTime ) ;
5848 } ,
59- [ setValue , getValues ] ,
49+ [ setValue ] ,
6050 ) ;
6151
6252 const onChangeCustomFormat = useCallback (
6353 ( event : ChangeEvent < HTMLInputElement > ) => {
6454 const value = event . target . value ;
6555 setValue ( 'customFormat' , value ) ;
66- const { inputDate, timezone } = getValues ( ) ;
67- setOutput ( {
68- ...convert ( inputDate , timezone ) ,
69- customFormat : customConvert ( inputDate , timezone , value ) ,
70- } ) ;
7156 } ,
72- [ getValues , setValue ] ,
57+ [ setValue ] ,
7358 ) ;
7459
60+ useEffect ( ( ) => {
61+ const { inputUnixTime, timezone, customFormat } = getValues ( ) ;
62+
63+ setOutput ( {
64+ ...convert ( inputUnixTime , timezone , customFormat ) ,
65+ } ) ;
66+ } , [ watch ( 'inputUnixTime' ) , watch ( 'timezone' ) , watch ( 'customFormat' ) ] ) ;
67+
7568 return {
7669 control,
7770 output,
@@ -82,8 +75,8 @@ export const useUnixTimeConverter = () => {
8275 } ;
8376} ;
8477
85- const convert = ( date : Dayjs , timezone : string ) => {
86- const dateTimezone = dayjs ( date ) . tz ( timezone ) ;
78+ const convert = ( unixtime : string , timezone : string , format : string ) => {
79+ const dateTimezone = dayjs ( unixtime ) . tz ( timezone ) ;
8780 const ISO8601 = dateTimezone . format ( 'YYYY-MM-DDTHH:mm:ssZ[Z]' ) ;
8881 const fullDate = dateTimezone . format ( 'YYYY/MM/DD HH:mm:ss' ) ;
8982 const enDate = dateTimezone . format ( 'LL' ) ;
@@ -92,7 +85,8 @@ const convert = (date: Dayjs, timezone: string) => {
9285 const month = dateTimezone . format ( 'MM' ) ;
9386 const d = dateTimezone . format ( 'DD' ) ;
9487 const week = dateTimezone . format ( 'dd' ) ;
95- const unixTime = dateTimezone . unix ( ) . toString ( ) ;
88+ const unixTime = unixtime ;
89+ const customFormat = dateTimezone . format ( format ) ;
9690
9791 return {
9892 ISO8601 ,
@@ -104,11 +98,7 @@ const convert = (date: Dayjs, timezone: string) => {
10498 week,
10599 unixTime,
106100 fullDate,
101+ customFormat,
107102 timezone,
108103 } ;
109104} ;
110-
111- const customConvert = ( date : Dayjs , timezone : string , format : string ) => {
112- const dateTimezone = dayjs ( date ) . tz ( timezone ) ;
113- return dateTimezone . format ( format ) ;
114- } ;
0 commit comments