Skip to content

Commit af9a2e0

Browse files
committed
Fix: 少しリファクタリング
1 parent f8f403c commit af9a2e0

2 files changed

Lines changed: 7 additions & 21 deletions

File tree

src/app/datetime_converter/DateTimeConverter.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,8 @@ const width = 300;
1818

1919
export const DateTimeConverter: FC = () => {
2020
const title = '日時->日時変換';
21-
const {
22-
methods,
23-
control,
24-
output,
25-
timezones,
26-
onChangeInputDate,
27-
onChangeTimezone,
28-
onChangeCustomFormat,
29-
} = useDateTimeConverter();
30-
const { timezone } = methods.getValues();
21+
const { control, output, timezones, onChangeInputDate, onChangeTimezone, onChangeCustomFormat } =
22+
useDateTimeConverter();
3123

3224
return (
3325
<AppLayout>

src/app/datetime_converter/useDateTimeConverter.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ export const useDateTimeConverter = () => {
3030
() => TIME_ZONES.map(({ label, tzCode }) => ({ label: label, value: tzCode })),
3131
[],
3232
);
33-
const timezone = watch('timezone') ?? 'UTC';
34-
const [output, setOutput] = useState<any>({});
33+
const [output, setOutput] = useState<ReturnType<typeof convert>>();
3534

3635
const onChangeInputDate = useCallback(
3736
(date: Dayjs) => {
@@ -58,8 +57,7 @@ export const useDateTimeConverter = () => {
5857
useEffect(() => {
5958
const { inputDate, timezone, customFormat } = getValues();
6059
setOutput({
61-
...convert(inputDate, timezone),
62-
customFormat: customConvert(inputDate, timezone, customFormat),
60+
...convert(inputDate, timezone, customFormat),
6361
});
6462
}, [watch('inputDate'), watch('timezone'), watch('customFormat')]);
6563

@@ -74,8 +72,7 @@ export const useDateTimeConverter = () => {
7472
};
7573
};
7674

77-
const convert = (date: Dayjs, timezone: string) => {
78-
console.log(date, timezone);
75+
const convert = (date: Dayjs, timezone: string, format: string) => {
7976
const dateTimezone = dayjs(date).tz(timezone);
8077
const ISO8601 = dateTimezone.format('YYYY-MM-DDTHH:mm:ssZ[Z]');
8178
const fullDate = dateTimezone.format('YYYY/MM/DD HH:mm:ss');
@@ -86,6 +83,7 @@ const convert = (date: Dayjs, timezone: string) => {
8683
const d = dateTimezone.format('DD');
8784
const week = dateTimezone.format('dd');
8885
const unixTime = dateTimezone.unix().toString();
86+
const customFormat = dateTimezone.format(format);
8987

9088
return {
9189
ISO8601,
@@ -97,11 +95,7 @@ const convert = (date: Dayjs, timezone: string) => {
9795
week,
9896
unixTime,
9997
fullDate,
98+
customFormat,
10099
timezone,
101100
};
102101
};
103-
104-
const customConvert = (date: Dayjs, timezone: string, format: string) => {
105-
const dateTimezone = dayjs(date).tz(timezone);
106-
return dateTimezone.format(format);
107-
};

0 commit comments

Comments
 (0)