From e83394e7bae347f4fac26d92d63e68c7aebe5ba2 Mon Sep 17 00:00:00 2001 From: lydiacho Date: Wed, 16 Oct 2024 23:13:53 +0900 Subject: [PATCH 01/13] =?UTF-8?q?feat:=20isBefore,=20isAfter=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 8 ++++++++ src/common/utils/dateTest.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/common/utils/dateFormatter.ts create mode 100644 src/common/utils/dateTest.ts diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts new file mode 100644 index 00000000..4dfce1a9 --- /dev/null +++ b/src/common/utils/dateFormatter.ts @@ -0,0 +1,8 @@ +export const _isBefore = (date: Date, dateToCompare: Date): boolean => { + return date.getTime() < dateToCompare.getTime(); +}; + +export const _isAfter = (date: Date, dateToCompare: Date): boolean => { + return date.getTime() > dateToCompare.getTime(); +}; +\ \ No newline at end of file diff --git a/src/common/utils/dateTest.ts b/src/common/utils/dateTest.ts new file mode 100644 index 00000000..b3749a83 --- /dev/null +++ b/src/common/utils/dateTest.ts @@ -0,0 +1,31 @@ +import { differenceInSeconds } from 'date-fns/differenceInSeconds'; +import { format } from 'date-fns/format'; +import { isAfter } from 'date-fns/isAfter'; +import { isBefore } from 'date-fns/isBefore'; +import { ko } from 'date-fns/locale/ko'; +import { subMinutes } from 'date-fns/subMinutes'; +import { _isAfter, _isBefore } from './dateFormatter'; + +const dateTest = () => { + const isBefore라브 = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); + const isBefore유틸 = _isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); + console.log('🚀isBefore Test:', isBefore라브 === isBefore유틸); + + const isAfter라브 = isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11)); + const isAfter유틸 = _isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11)); + console.log('🚀isAfter Test:', isAfter라브 === isAfter유틸); + + const result3 = differenceInSeconds(new Date(2014, 6, 2, 12, 30, 20, 0), new Date(2014, 6, 2, 12, 30, 7, 999)); + console.log('🚀differenceInSeconds:', result3); //=> 12 + + const result4 = subMinutes(new Date(2014, 6, 10, 12, 0), 30); + console.log('🚀subMinutes:', result4); //=> Thu Jul 10 2014 11:30:00 + + const result5 = format(new Date(2014, 1, 11, 6), 'M월 dd일 (E) aaa HH시', { locale: ko }); + // const formattedApplicationStart = format(new Date(applicationStart || ''), 'M월 dd일 (E) aaa HH시 mm분', { + // locale: ko, + // }); + console.log('🚀format:', result5); //=> '02/11/2014'; +}; + +export default dateTest; From 3a672656872fb3937e6d8242139f508cb7185fc3 Mon Sep 17 00:00:00 2001 From: lydiacho Date: Wed, 16 Oct 2024 23:24:42 +0900 Subject: [PATCH 02/13] =?UTF-8?q?feat:=20differenceInSeconds=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 5 ++++- src/common/utils/dateTest.ts | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index 4dfce1a9..50562846 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -5,4 +5,7 @@ export const _isBefore = (date: Date, dateToCompare: Date): boolean => { export const _isAfter = (date: Date, dateToCompare: Date): boolean => { return date.getTime() > dateToCompare.getTime(); }; -\ \ No newline at end of file + +export const _differenceInSeconds = (laterDate: Date, earlierDate: Date): number => { + return Math.floor((laterDate.getTime() - earlierDate.getTime()) / 1000); +}; diff --git a/src/common/utils/dateTest.ts b/src/common/utils/dateTest.ts index b3749a83..cb76038f 100644 --- a/src/common/utils/dateTest.ts +++ b/src/common/utils/dateTest.ts @@ -4,7 +4,7 @@ import { isAfter } from 'date-fns/isAfter'; import { isBefore } from 'date-fns/isBefore'; import { ko } from 'date-fns/locale/ko'; import { subMinutes } from 'date-fns/subMinutes'; -import { _isAfter, _isBefore } from './dateFormatter'; +import { _differenceInSeconds, _isAfter, _isBefore } from './dateFormatter'; const dateTest = () => { const isBefore라브 = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); @@ -15,8 +15,9 @@ const dateTest = () => { const isAfter유틸 = _isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11)); console.log('🚀isAfter Test:', isAfter라브 === isAfter유틸); - const result3 = differenceInSeconds(new Date(2014, 6, 2, 12, 30, 20, 0), new Date(2014, 6, 2, 12, 30, 7, 999)); - console.log('🚀differenceInSeconds:', result3); //=> 12 + const dIS라브 = differenceInSeconds(new Date(2014, 6, 2, 12, 30, 20, 700), new Date(2014, 6, 2, 12, 30, 7, 999)); + const dIS유틸 = _differenceInSeconds(new Date(2014, 6, 2, 12, 30, 20, 700), new Date(2014, 6, 2, 12, 30, 7, 999)); + console.log('🚀differenceInSeconds Test:', dIS라브 === dIS유틸); const result4 = subMinutes(new Date(2014, 6, 10, 12, 0), 30); console.log('🚀subMinutes:', result4); //=> Thu Jul 10 2014 11:30:00 From b98d32b73ce5d9f4de7bddf8df6f0d6a2019fa98 Mon Sep 17 00:00:00 2001 From: lydiacho Date: Wed, 16 Oct 2024 23:40:17 +0900 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20subMinutes=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 6 ++++++ src/common/utils/dateTest.ts | 7 ++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index 50562846..3bc93403 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -9,3 +9,9 @@ export const _isAfter = (date: Date, dateToCompare: Date): boolean => { export const _differenceInSeconds = (laterDate: Date, earlierDate: Date): number => { return Math.floor((laterDate.getTime() - earlierDate.getTime()) / 1000); }; + +export const _subMinutes = (date: Date, amount: number): Date => { + const newDate = new Date(); + newDate.setTime(date.getTime() - amount * 60 * 1000); + return newDate; +}; diff --git a/src/common/utils/dateTest.ts b/src/common/utils/dateTest.ts index cb76038f..ba40ce75 100644 --- a/src/common/utils/dateTest.ts +++ b/src/common/utils/dateTest.ts @@ -4,7 +4,7 @@ import { isAfter } from 'date-fns/isAfter'; import { isBefore } from 'date-fns/isBefore'; import { ko } from 'date-fns/locale/ko'; import { subMinutes } from 'date-fns/subMinutes'; -import { _differenceInSeconds, _isAfter, _isBefore } from './dateFormatter'; +import { _differenceInSeconds, _isAfter, _isBefore, _subMinutes } from './dateFormatter'; const dateTest = () => { const isBefore라브 = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); @@ -19,8 +19,9 @@ const dateTest = () => { const dIS유틸 = _differenceInSeconds(new Date(2014, 6, 2, 12, 30, 20, 700), new Date(2014, 6, 2, 12, 30, 7, 999)); console.log('🚀differenceInSeconds Test:', dIS라브 === dIS유틸); - const result4 = subMinutes(new Date(2014, 6, 10, 12, 0), 30); - console.log('🚀subMinutes:', result4); //=> Thu Jul 10 2014 11:30:00 + const subMinutes라브 = subMinutes(new Date(2014, 6, 10, 12, 0), 30); + const subMinutes유틸 = _subMinutes(new Date(2014, 6, 10, 12, 0), 30); + console.log('🚀subMinutes Test:', subMinutes라브.getTime() === subMinutes유틸.getTime()); const result5 = format(new Date(2014, 1, 11, 6), 'M월 dd일 (E) aaa HH시', { locale: ko }); // const formattedApplicationStart = format(new Date(applicationStart || ''), 'M월 dd일 (E) aaa HH시 mm분', { From 4f16fb89bbcce8334ffe285d321795765f8a706d Mon Sep 17 00:00:00 2001 From: lydiacho Date: Thu, 17 Oct 2024 01:20:30 +0900 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20format=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 29 +++++++++++++++++++++++++++++ src/common/utils/dateTest.ts | 20 ++++++++++++++------ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index 3bc93403..c8cfe2f6 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -15,3 +15,32 @@ export const _subMinutes = (date: Date, amount: number): Date => { newDate.setTime(date.getTime() - amount * 60 * 1000); return newDate; }; + +export const _format = (date: Date, formatStr: string): string => { + const days = ['월', '화', '수', '목', '금', '토', '일']; + const formatter: { [key: string]: string } = { + M: (date.getMonth() + 1).toString(), + dd: date.getDate().toString().padStart(2, '0'), + E: days[date.getDay() - 1], + aaa: date.getHours() < 12 ? '오전' : '오후', + HH: date.getHours().toString().padStart(2, '0'), + hh: (date.getHours() % 12 || 12).toString().padStart(2, '0'), + mm: date.getMinutes().toString().padStart(2, '0'), + }; + + return formatStr.replace(/M|dd|E|aaa|HH|hh|mm/g, (substr) => formatter[substr]); +}; + +interface formatIntlOptions { + year: 'numeric' | '2-digit'; + month: 'numeric' | 'long'; + day: 'numeric' | '2-digit'; + weekday: 'long' | 'short' | 'narrow'; + // 오전 오후 표시할 수 있는 건 없네... + hour: 'numeric' | '2-digit'; + minute: 'numeric' | '2-digit'; +} + +export const _formatIntl = (date: Date, options: Partial): string => { + return new Intl.DateTimeFormat('ko-KR', { ...options, timeZone: 'Asia/Seoul' }).format(date); +}; diff --git a/src/common/utils/dateTest.ts b/src/common/utils/dateTest.ts index ba40ce75..2d79575f 100644 --- a/src/common/utils/dateTest.ts +++ b/src/common/utils/dateTest.ts @@ -4,7 +4,7 @@ import { isAfter } from 'date-fns/isAfter'; import { isBefore } from 'date-fns/isBefore'; import { ko } from 'date-fns/locale/ko'; import { subMinutes } from 'date-fns/subMinutes'; -import { _differenceInSeconds, _isAfter, _isBefore, _subMinutes } from './dateFormatter'; +import { _differenceInSeconds, _format, _formatIntl, _isAfter, _isBefore, _subMinutes } from './dateFormatter'; const dateTest = () => { const isBefore라브 = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); @@ -23,11 +23,19 @@ const dateTest = () => { const subMinutes유틸 = _subMinutes(new Date(2014, 6, 10, 12, 0), 30); console.log('🚀subMinutes Test:', subMinutes라브.getTime() === subMinutes유틸.getTime()); - const result5 = format(new Date(2014, 1, 11, 6), 'M월 dd일 (E) aaa HH시', { locale: ko }); - // const formattedApplicationStart = format(new Date(applicationStart || ''), 'M월 dd일 (E) aaa HH시 mm분', { - // locale: ko, - // }); - console.log('🚀format:', result5); //=> '02/11/2014'; + const format라브 = format(new Date('2024-06-20T15:27:45.000Z'), 'M월 dd일 (E) aaa HH시 mm분', { locale: ko }); + const format유틸 = _format(new Date('2024-06-20T15:27:45.000Z'), 'M월 dd일 (E) aaa HH시 mm분'); + const formatIntl = _formatIntl(new Date('2024-06-20T15:27:45.000Z'), { + month: 'long', + day: '2-digit', + weekday: 'short', + hour: '2-digit', + minute: '2-digit', + }); + console.log('🚀format by lib: ', format라브); + console.log('🚀format by util: ', format유틸); + console.log('🚀format by Intl: ', formatIntl); + console.log('🚀format Test:', format유틸 === format라브); }; export default dateTest; From 63afc4aef876ecc78b0b3be437b0733c22737e4f Mon Sep 17 00:00:00 2001 From: lydiacho Date: Thu, 17 Oct 2024 01:24:29 +0900 Subject: [PATCH 05/13] =?UTF-8?q?remove:=20Intl=20=EB=8D=9C=EC=96=B4?= =?UTF-8?q?=EB=82=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 14 -------------- src/common/utils/dateTest.ts | 16 +++++----------- 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index c8cfe2f6..1bf6ca49 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -30,17 +30,3 @@ export const _format = (date: Date, formatStr: string): string => { return formatStr.replace(/M|dd|E|aaa|HH|hh|mm/g, (substr) => formatter[substr]); }; - -interface formatIntlOptions { - year: 'numeric' | '2-digit'; - month: 'numeric' | 'long'; - day: 'numeric' | '2-digit'; - weekday: 'long' | 'short' | 'narrow'; - // 오전 오후 표시할 수 있는 건 없네... - hour: 'numeric' | '2-digit'; - minute: 'numeric' | '2-digit'; -} - -export const _formatIntl = (date: Date, options: Partial): string => { - return new Intl.DateTimeFormat('ko-KR', { ...options, timeZone: 'Asia/Seoul' }).format(date); -}; diff --git a/src/common/utils/dateTest.ts b/src/common/utils/dateTest.ts index 2d79575f..edcadcb9 100644 --- a/src/common/utils/dateTest.ts +++ b/src/common/utils/dateTest.ts @@ -4,9 +4,11 @@ import { isAfter } from 'date-fns/isAfter'; import { isBefore } from 'date-fns/isBefore'; import { ko } from 'date-fns/locale/ko'; import { subMinutes } from 'date-fns/subMinutes'; -import { _differenceInSeconds, _format, _formatIntl, _isAfter, _isBefore, _subMinutes } from './dateFormatter'; +import { _differenceInSeconds, _format, _isAfter, _isBefore, _subMinutes } from './dateFormatter'; const dateTest = () => { + const sampleDate = '2024-06-20T15:27:45.000Z'; + const isBefore라브 = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); const isBefore유틸 = _isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); console.log('🚀isBefore Test:', isBefore라브 === isBefore유틸); @@ -23,18 +25,10 @@ const dateTest = () => { const subMinutes유틸 = _subMinutes(new Date(2014, 6, 10, 12, 0), 30); console.log('🚀subMinutes Test:', subMinutes라브.getTime() === subMinutes유틸.getTime()); - const format라브 = format(new Date('2024-06-20T15:27:45.000Z'), 'M월 dd일 (E) aaa HH시 mm분', { locale: ko }); - const format유틸 = _format(new Date('2024-06-20T15:27:45.000Z'), 'M월 dd일 (E) aaa HH시 mm분'); - const formatIntl = _formatIntl(new Date('2024-06-20T15:27:45.000Z'), { - month: 'long', - day: '2-digit', - weekday: 'short', - hour: '2-digit', - minute: '2-digit', - }); + const format라브 = format(new Date(sampleDate), 'M월 dd일 (E) aaa HH시 mm분', { locale: ko }); + const format유틸 = _format(new Date(sampleDate), 'M월 dd일 (E) aaa HH시 mm분'); console.log('🚀format by lib: ', format라브); console.log('🚀format by util: ', format유틸); - console.log('🚀format by Intl: ', formatIntl); console.log('🚀format Test:', format유틸 === format라브); }; From 7374bdbf3ae09e8a43f78a29287011af81674b87 Mon Sep 17 00:00:00 2001 From: lydiacho Date: Thu, 17 Oct 2024 01:38:29 +0900 Subject: [PATCH 06/13] =?UTF-8?q?feat:=20string=20->=20Date=20=ED=98=95?= =?UTF-8?q?=EB=B3=80=ED=99=98=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 18 +++++++++++++----- src/common/utils/dateTest.ts | 22 ++++++++++------------ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index 1bf6ca49..aac4a58a 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -1,22 +1,30 @@ -export const _isBefore = (date: Date, dateToCompare: Date): boolean => { +export const _isBefore = (date: Date | string, dateToCompare: Date | string): boolean => { + if (typeof date === 'string') date = new Date(date); + if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare); return date.getTime() < dateToCompare.getTime(); }; -export const _isAfter = (date: Date, dateToCompare: Date): boolean => { +export const _isAfter = (date: Date | string, dateToCompare: Date | string): boolean => { + if (typeof date === 'string') date = new Date(date); + if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare); return date.getTime() > dateToCompare.getTime(); }; -export const _differenceInSeconds = (laterDate: Date, earlierDate: Date): number => { +export const _differenceInSeconds = (laterDate: Date | string, earlierDate: Date | string): number => { + if (typeof laterDate === 'string') laterDate = new Date(laterDate); + if (typeof earlierDate === 'string') earlierDate = new Date(earlierDate); return Math.floor((laterDate.getTime() - earlierDate.getTime()) / 1000); }; -export const _subMinutes = (date: Date, amount: number): Date => { +export const _subMinutes = (date: Date | string, amount: number): Date => { + if (typeof date === 'string') date = new Date(date); const newDate = new Date(); newDate.setTime(date.getTime() - amount * 60 * 1000); return newDate; }; -export const _format = (date: Date, formatStr: string): string => { +export const _format = (date: Date | string, formatStr: string): string => { + if (typeof date === 'string') date = new Date(date); const days = ['월', '화', '수', '목', '금', '토', '일']; const formatter: { [key: string]: string } = { M: (date.getMonth() + 1).toString(), diff --git a/src/common/utils/dateTest.ts b/src/common/utils/dateTest.ts index edcadcb9..ce89a267 100644 --- a/src/common/utils/dateTest.ts +++ b/src/common/utils/dateTest.ts @@ -9,26 +9,24 @@ import { _differenceInSeconds, _format, _isAfter, _isBefore, _subMinutes } from const dateTest = () => { const sampleDate = '2024-06-20T15:27:45.000Z'; - const isBefore라브 = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); - const isBefore유틸 = _isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)); + const isBefore라브 = isBefore(sampleDate, new Date(1987, 1, 11)); + const isBefore유틸 = _isBefore(sampleDate, new Date(1987, 1, 11)); console.log('🚀isBefore Test:', isBefore라브 === isBefore유틸); - const isAfter라브 = isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11)); - const isAfter유틸 = _isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11)); + const isAfter라브 = isAfter(sampleDate, new Date(1987, 1, 11)); + const isAfter유틸 = _isAfter(sampleDate, new Date(1987, 1, 11)); console.log('🚀isAfter Test:', isAfter라브 === isAfter유틸); - const dIS라브 = differenceInSeconds(new Date(2014, 6, 2, 12, 30, 20, 700), new Date(2014, 6, 2, 12, 30, 7, 999)); - const dIS유틸 = _differenceInSeconds(new Date(2014, 6, 2, 12, 30, 20, 700), new Date(2014, 6, 2, 12, 30, 7, 999)); + const dIS라브 = differenceInSeconds(sampleDate, new Date(2014, 6, 2, 12, 30, 7, 999)); + const dIS유틸 = _differenceInSeconds(sampleDate, new Date(2014, 6, 2, 12, 30, 7, 999)); console.log('🚀differenceInSeconds Test:', dIS라브 === dIS유틸); - const subMinutes라브 = subMinutes(new Date(2014, 6, 10, 12, 0), 30); - const subMinutes유틸 = _subMinutes(new Date(2014, 6, 10, 12, 0), 30); + const subMinutes라브 = subMinutes(new Date(sampleDate), 30); + const subMinutes유틸 = _subMinutes(new Date(sampleDate), 30); console.log('🚀subMinutes Test:', subMinutes라브.getTime() === subMinutes유틸.getTime()); - const format라브 = format(new Date(sampleDate), 'M월 dd일 (E) aaa HH시 mm분', { locale: ko }); - const format유틸 = _format(new Date(sampleDate), 'M월 dd일 (E) aaa HH시 mm분'); - console.log('🚀format by lib: ', format라브); - console.log('🚀format by util: ', format유틸); + const format라브 = format(sampleDate, 'M월 dd일 (E) aaa HH시 mm분', { locale: ko }); + const format유틸 = _format(sampleDate, 'M월 dd일 (E) aaa HH시 mm분'); console.log('🚀format Test:', format유틸 === format라브); }; From ceb709bcd98bb84cbd4d916ca698bc8895d9b0f0 Mon Sep 17 00:00:00 2001 From: lydiacho Date: Thu, 17 Oct 2024 01:39:51 +0900 Subject: [PATCH 07/13] =?UTF-8?q?remove:=20test=20=EC=9A=A9=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateTest.ts | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 src/common/utils/dateTest.ts diff --git a/src/common/utils/dateTest.ts b/src/common/utils/dateTest.ts deleted file mode 100644 index ce89a267..00000000 --- a/src/common/utils/dateTest.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { differenceInSeconds } from 'date-fns/differenceInSeconds'; -import { format } from 'date-fns/format'; -import { isAfter } from 'date-fns/isAfter'; -import { isBefore } from 'date-fns/isBefore'; -import { ko } from 'date-fns/locale/ko'; -import { subMinutes } from 'date-fns/subMinutes'; -import { _differenceInSeconds, _format, _isAfter, _isBefore, _subMinutes } from './dateFormatter'; - -const dateTest = () => { - const sampleDate = '2024-06-20T15:27:45.000Z'; - - const isBefore라브 = isBefore(sampleDate, new Date(1987, 1, 11)); - const isBefore유틸 = _isBefore(sampleDate, new Date(1987, 1, 11)); - console.log('🚀isBefore Test:', isBefore라브 === isBefore유틸); - - const isAfter라브 = isAfter(sampleDate, new Date(1987, 1, 11)); - const isAfter유틸 = _isAfter(sampleDate, new Date(1987, 1, 11)); - console.log('🚀isAfter Test:', isAfter라브 === isAfter유틸); - - const dIS라브 = differenceInSeconds(sampleDate, new Date(2014, 6, 2, 12, 30, 7, 999)); - const dIS유틸 = _differenceInSeconds(sampleDate, new Date(2014, 6, 2, 12, 30, 7, 999)); - console.log('🚀differenceInSeconds Test:', dIS라브 === dIS유틸); - - const subMinutes라브 = subMinutes(new Date(sampleDate), 30); - const subMinutes유틸 = _subMinutes(new Date(sampleDate), 30); - console.log('🚀subMinutes Test:', subMinutes라브.getTime() === subMinutes유틸.getTime()); - - const format라브 = format(sampleDate, 'M월 dd일 (E) aaa HH시 mm분', { locale: ko }); - const format유틸 = _format(sampleDate, 'M월 dd일 (E) aaa HH시 mm분'); - console.log('🚀format Test:', format유틸 === format라브); -}; - -export default dateTest; From e62d6d695fbe05da67dc6a829532bc1125b53bfe Mon Sep 17 00:00:00 2001 From: lydiacho Date: Thu, 17 Oct 2024 01:47:48 +0900 Subject: [PATCH 08/13] =?UTF-8?q?remove:=20date-fns=20=EB=9D=BC=EC=9D=B4?= =?UTF-8?q?=EB=B8=8C=EB=9F=AC=EB=A6=AC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +-- src/common/apis/tokenInstance.ts | 2 +- .../Input/components/Timer/index.tsx | 2 +- src/common/hooks/useDate.tsx | 3 +-- src/common/utils/dateFormatter.ts | 10 ++++----- .../ApplyPage/components/ApplyInfo/index.tsx | 21 ++++++------------- .../ResultPage/components/FinalResult.tsx | 7 ++----- .../ResultPage/components/ScreeningResult.tsx | 15 +++++-------- yarn.lock | 5 ----- 9 files changed, 22 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index e3379f91..56252cae 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "@vanilla-extract/css": "^1.15.1", "@vanilla-extract/css-utils": "^0.1.4", "axios": "^1.6.8", - "date-fns": "^3.6.0", "firebase": "^10.12.4", "lottie-react": "^2.4.0", "nanoid": "^5.0.7", @@ -86,4 +85,4 @@ "vite": "^5.2.0", "vitest": "^2.0.5" } -} \ No newline at end of file +} diff --git a/src/common/apis/tokenInstance.ts b/src/common/apis/tokenInstance.ts index 2b1e6c16..6eb9ce5e 100644 --- a/src/common/apis/tokenInstance.ts +++ b/src/common/apis/tokenInstance.ts @@ -1,5 +1,5 @@ +import { isBefore } from '@utils/dateFormatter'; import axios from 'axios'; -import { isBefore } from 'date-fns/isBefore'; const tokenInstance = axios.create({ baseURL: import.meta.env.VITE_BASE_URL, diff --git a/src/common/components/Input/components/Timer/index.tsx b/src/common/components/Input/components/Timer/index.tsx index 2fa854f7..0eb3ee06 100644 --- a/src/common/components/Input/components/Timer/index.tsx +++ b/src/common/components/Input/components/Timer/index.tsx @@ -1,4 +1,3 @@ -import { differenceInSeconds } from 'date-fns/differenceInSeconds'; import { useEffect, useState } from 'react'; import { useDeviceType } from 'contexts/DeviceTypeProvider'; @@ -6,6 +5,7 @@ import { useDeviceType } from 'contexts/DeviceTypeProvider'; import { timerVar } from './style.css'; import { TimerProps } from './types'; import formatTimer from './utils/formatTimer'; +import { differenceInSeconds } from '@utils/dateFormatter'; const INITIAL_TIME = 300; diff --git a/src/common/hooks/useDate.tsx b/src/common/hooks/useDate.tsx index 026da42b..489a5187 100644 --- a/src/common/hooks/useDate.tsx +++ b/src/common/hooks/useDate.tsx @@ -1,10 +1,9 @@ -import { isAfter } from 'date-fns/isAfter'; -import { isBefore } from 'date-fns/isBefore'; import { useEffect } from 'react'; import { useRecruitingInfo } from 'contexts/RecruitingInfoProvider'; import useGetRecruitingInfo from './useGetRecruitingInfo'; +import { isAfter, isBefore } from '@utils/dateFormatter'; const useDate = () => { const { handleSaveRecruitingInfo } = useRecruitingInfo(); diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index aac4a58a..5fe1ddf7 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -1,29 +1,29 @@ -export const _isBefore = (date: Date | string, dateToCompare: Date | string): boolean => { +export const isBefore = (date: Date | string, dateToCompare: Date | string): boolean => { if (typeof date === 'string') date = new Date(date); if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare); return date.getTime() < dateToCompare.getTime(); }; -export const _isAfter = (date: Date | string, dateToCompare: Date | string): boolean => { +export const isAfter = (date: Date | string, dateToCompare: Date | string): boolean => { if (typeof date === 'string') date = new Date(date); if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare); return date.getTime() > dateToCompare.getTime(); }; -export const _differenceInSeconds = (laterDate: Date | string, earlierDate: Date | string): number => { +export const differenceInSeconds = (laterDate: Date | string, earlierDate: Date | string): number => { if (typeof laterDate === 'string') laterDate = new Date(laterDate); if (typeof earlierDate === 'string') earlierDate = new Date(earlierDate); return Math.floor((laterDate.getTime() - earlierDate.getTime()) / 1000); }; -export const _subMinutes = (date: Date | string, amount: number): Date => { +export const subMinutes = (date: Date | string, amount: number): Date => { if (typeof date === 'string') date = new Date(date); const newDate = new Date(); newDate.setTime(date.getTime() - amount * 60 * 1000); return newDate; }; -export const _format = (date: Date | string, formatStr: string): string => { +export const format = (date: Date | string, formatStr: string): string => { if (typeof date === 'string') date = new Date(date); const days = ['월', '화', '수', '목', '금', '토', '일']; const formatter: { [key: string]: string } = { diff --git a/src/views/ApplyPage/components/ApplyInfo/index.tsx b/src/views/ApplyPage/components/ApplyInfo/index.tsx index 7bbec85a..305905d2 100644 --- a/src/views/ApplyPage/components/ApplyInfo/index.tsx +++ b/src/views/ApplyPage/components/ApplyInfo/index.tsx @@ -1,6 +1,3 @@ -import { format } from 'date-fns/format'; -import { ko } from 'date-fns/locale/ko'; -import { subMinutes } from 'date-fns/subMinutes'; import { memo } from 'react'; import Callout from '@components/Callout'; @@ -18,6 +15,7 @@ import { infoWrapperVar, } from './style.css'; import { APPLY_INFO } from '../../constant'; +import { format, subMinutes } from '@utils/dateFormatter'; const ApplyInfo = memo(({ isReview = false }: { isReview?: boolean }) => { const { deviceType } = useDeviceType(); @@ -34,22 +32,15 @@ const ApplyInfo = memo(({ isReview = false }: { isReview?: boolean }) => { if (!applicationStart) return; - const formattedApplicationStart = format(new Date(applicationStart || ''), 'M월 dd일 (E) aaa HH시 mm분', { - locale: ko, - }); - const formattedApplicationEnd = format(subMinutes(new Date(applicationEnd || ''), 1), 'M월 dd일 (E) aaa HH시 mm분', { - locale: ko, - }); + const formattedApplicationStart = format(new Date(applicationStart || ''), 'M월 dd일 (E) aaa HH시 mm분'); + const formattedApplicationEnd = format(subMinutes(new Date(applicationEnd || ''), 1), 'M월 dd일 (E) aaa HH시 mm분'); const formattedApplicationConfirmStart = format( new Date(applicationPassConfirmStart || ''), 'M월 dd일 (E) aaa HH시 mm분', - { - locale: ko, - }, ); - const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일 (E)', { locale: ko }); - const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일 (E)', { locale: ko }); - const formattedFinalPassConfirmStart = format(new Date(finalPassConfirmStart || ''), 'M월 dd일 (E)', { locale: ko }); + const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일 (E)'); + const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일 (E)'); + const formattedFinalPassConfirmStart = format(new Date(finalPassConfirmStart || ''), 'M월 dd일 (E)'); return (
diff --git a/src/views/ResultPage/components/FinalResult.tsx b/src/views/ResultPage/components/FinalResult.tsx index 0424de81..c73947b0 100644 --- a/src/views/ResultPage/components/FinalResult.tsx +++ b/src/views/ResultPage/components/FinalResult.tsx @@ -1,6 +1,4 @@ import { track } from '@amplitude/analytics-browser'; -import { format } from 'date-fns/format'; -import { ko } from 'date-fns/locale/ko'; import { useEffect } from 'react'; import Title from '@components/Title'; @@ -23,6 +21,7 @@ import IconMakersLogo from '../assets/IconMakersLogo'; import imgSoptLogo from '../assets/imgSoptLogo.png'; import imgSoptLogoWebp from '../assets/imgSoptLogo.webp'; import useGetFinalResult from '../hooks/useGetFinalResult'; +import { format } from '@utils/dateFormatter'; const Content = ({ pass }: { pass?: boolean }) => { const { deviceType } = useDeviceType(); @@ -33,9 +32,7 @@ const Content = ({ pass }: { pass?: boolean }) => { if (!name) return; const finalDate = new Date(finalPassConfirmStart || ''); - const formattedFinalPassConfirmStart = format(finalDate, 'M월 dd일 EEEE', { - locale: ko, - }); + const formattedFinalPassConfirmStart = format(finalDate, 'M월 dd일 EEEE'); const SOPT_NAME = isMakers ? `SOPT ${soptName}` : soptName; return ( diff --git a/src/views/ResultPage/components/ScreeningResult.tsx b/src/views/ResultPage/components/ScreeningResult.tsx index fdbeab74..6ab38fbb 100644 --- a/src/views/ResultPage/components/ScreeningResult.tsx +++ b/src/views/ResultPage/components/ScreeningResult.tsx @@ -1,6 +1,4 @@ import { track } from '@amplitude/analytics-browser'; -import { format } from 'date-fns/format'; -import { ko } from 'date-fns/locale/ko'; import { useEffect } from 'react'; import Title from '@components/Title'; @@ -23,6 +21,7 @@ import IconMakersLogo from '../assets/IconMakersLogo'; import imgSoptLogo from '../assets/imgSoptLogo.png'; import imgSoptLogoWebp from '../assets/imgSoptLogo.webp'; import useGetScreeningResult from '../hooks/useGetScreeningResult'; +import { format } from '@utils/dateFormatter'; const Content = ({ pass }: { pass?: boolean }) => { const { deviceType } = useDeviceType(); @@ -36,14 +35,10 @@ const Content = ({ pass }: { pass?: boolean }) => { const applicationPassConfirmNextDay = new Date(applicationDate); applicationPassConfirmNextDay.setDate(applicationDate.getDate() + 1); - const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일', { locale: ko }); - const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일', { locale: ko }); - const formattedApplicationPassConfirmStart = format(applicationDate, 'M월 dd일 EEEE', { - locale: ko, - }); - const formattedApplicationPassConfirmNextDay = format(new Date(applicationPassConfirmNextDay || ''), 'M월 dd일', { - locale: ko, - }); + const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일'); + const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일'); + const formattedApplicationPassConfirmStart = format(applicationDate, 'M월 dd일 EEEE'); + const formattedApplicationPassConfirmNextDay = format(new Date(applicationPassConfirmNextDay || ''), 'M월 dd일'); const SOPT_NAME = isMakers ? `SOPT ${soptName}` : soptName; return ( diff --git a/yarn.lock b/yarn.lock index 278562a2..784d0191 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2514,11 +2514,6 @@ data-view-byte-offset@^1.0.0: es-errors "^1.3.0" is-data-view "^1.0.1" -date-fns@^3.6.0: - version "3.6.0" - resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-3.6.0.tgz#f20ca4fe94f8b754951b24240676e8618c0206bf" - integrity sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww== - debug@4, debug@^4.3.5: version "4.3.6" resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.6.tgz#2ab2c38fbaffebf8aa95fdfe6d88438c7a13c52b" From 2075ced75fa18f3ddee8f7b4f172f7cc410dd231 Mon Sep 17 00:00:00 2001 From: lydiacho Date: Thu, 17 Oct 2024 01:48:59 +0900 Subject: [PATCH 09/13] =?UTF-8?q?feat:=20EEEE=20RegExp=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index 5fe1ddf7..7df6c2eb 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -30,11 +30,12 @@ export const format = (date: Date | string, formatStr: string): string => { M: (date.getMonth() + 1).toString(), dd: date.getDate().toString().padStart(2, '0'), E: days[date.getDay() - 1], + EEEE: days[date.getDay() - 1] + '요일', aaa: date.getHours() < 12 ? '오전' : '오후', HH: date.getHours().toString().padStart(2, '0'), hh: (date.getHours() % 12 || 12).toString().padStart(2, '0'), mm: date.getMinutes().toString().padStart(2, '0'), }; - return formatStr.replace(/M|dd|E|aaa|HH|hh|mm/g, (substr) => formatter[substr]); + return formatStr.replace(/M|dd|E|EEE|aaa|HH|hh|mm/g, (substr) => formatter[substr]); }; From ac856e302332d105bb3ce32b2e7a2118e50a462c Mon Sep 17 00:00:00 2001 From: lydiacho Date: Fri, 18 Oct 2024 16:11:41 +0900 Subject: [PATCH 10/13] =?UTF-8?q?fix:=20toDate=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index 7df6c2eb..129907bb 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -1,3 +1,11 @@ +const toDate = (date: Date | string): Date => { + const newDate = typeof date === 'string' ? new Date(date) : date; + if (isNaN(newDate.getTime())) { + throw new Error('변환할 수 없는 날짜입니다.'); + } + return newDate; +}; + export const isBefore = (date: Date | string, dateToCompare: Date | string): boolean => { if (typeof date === 'string') date = new Date(date); if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare); From 9074cfc520e5e330a1f0b6ff67f7cbb5a351c58a Mon Sep 17 00:00:00 2001 From: lydiacho Date: Fri, 18 Oct 2024 16:13:09 +0900 Subject: [PATCH 11/13] =?UTF-8?q?fix:=20=EC=9A=94=EC=9D=BC=20=EC=9D=B8?= =?UTF-8?q?=EB=8D=B1=EC=8A=A4=20=EB=B2=84=EA=B7=B8=20=ED=94=BD=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index 129907bb..cd28ec3a 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -33,12 +33,12 @@ export const subMinutes = (date: Date | string, amount: number): Date => { export const format = (date: Date | string, formatStr: string): string => { if (typeof date === 'string') date = new Date(date); - const days = ['월', '화', '수', '목', '금', '토', '일']; + const days = ['일', '월', '화', '수', '목', '금', '토']; const formatter: { [key: string]: string } = { M: (date.getMonth() + 1).toString(), dd: date.getDate().toString().padStart(2, '0'), - E: days[date.getDay() - 1], - EEEE: days[date.getDay() - 1] + '요일', + E: days[date.getDay()], + EEEE: days[date.getDay()] + '요일', aaa: date.getHours() < 12 ? '오전' : '오후', HH: date.getHours().toString().padStart(2, '0'), hh: (date.getHours() % 12 || 12).toString().padStart(2, '0'), From 215b287af873659474a549468b9c1982e939733e Mon Sep 17 00:00:00 2001 From: lydiacho Date: Fri, 18 Oct 2024 16:32:13 +0900 Subject: [PATCH 12/13] =?UTF-8?q?fix:=20toDate=20=EC=A0=81=EC=9A=A9=20?= =?UTF-8?q?=EB=B0=8F=20throw=20Error=20->=20console=20error=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index cd28ec3a..efffc477 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -1,38 +1,37 @@ const toDate = (date: Date | string): Date => { const newDate = typeof date === 'string' ? new Date(date) : date; if (isNaN(newDate.getTime())) { - throw new Error('변환할 수 없는 날짜입니다.'); + console.error(`${date} is invalid date.`); } return newDate; }; export const isBefore = (date: Date | string, dateToCompare: Date | string): boolean => { - if (typeof date === 'string') date = new Date(date); - if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare); - return date.getTime() < dateToCompare.getTime(); + return toDate(date).getTime() < toDate(dateToCompare).getTime(); }; export const isAfter = (date: Date | string, dateToCompare: Date | string): boolean => { - if (typeof date === 'string') date = new Date(date); - if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare); - return date.getTime() > dateToCompare.getTime(); + return toDate(date).getTime() > toDate(dateToCompare).getTime(); }; export const differenceInSeconds = (laterDate: Date | string, earlierDate: Date | string): number => { - if (typeof laterDate === 'string') laterDate = new Date(laterDate); - if (typeof earlierDate === 'string') earlierDate = new Date(earlierDate); - return Math.floor((laterDate.getTime() - earlierDate.getTime()) / 1000); + return Math.floor((toDate(laterDate).getTime() - toDate(earlierDate).getTime()) / 1000); }; export const subMinutes = (date: Date | string, amount: number): Date => { - if (typeof date === 'string') date = new Date(date); const newDate = new Date(); - newDate.setTime(date.getTime() - amount * 60 * 1000); + newDate.setTime(toDate(date).getTime() - amount * 60 * 1000); return newDate; }; +export const _subMinutes = (date: Date | string, amount: number): Date => { + date = toDate(date); + date.setTime(date.getTime() - amount * 60 * 1000); + return date; +}; + export const format = (date: Date | string, formatStr: string): string => { - if (typeof date === 'string') date = new Date(date); + date = toDate(date); const days = ['일', '월', '화', '수', '목', '금', '토']; const formatter: { [key: string]: string } = { M: (date.getMonth() + 1).toString(), From ab2d87d0f76549a2480ed5df44e952f20abd861e Mon Sep 17 00:00:00 2001 From: lydiacho Date: Mon, 21 Oct 2024 19:17:41 +0900 Subject: [PATCH 13/13] =?UTF-8?q?fix:=20throw=20Error=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EB=B3=B5=EA=B5=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/dateFormatter.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common/utils/dateFormatter.ts b/src/common/utils/dateFormatter.ts index efffc477..d8dd135e 100644 --- a/src/common/utils/dateFormatter.ts +++ b/src/common/utils/dateFormatter.ts @@ -1,7 +1,8 @@ const toDate = (date: Date | string): Date => { const newDate = typeof date === 'string' ? new Date(date) : date; if (isNaN(newDate.getTime())) { - console.error(`${date} is invalid date.`); + // TODO : 배포 시 주석 해제 + // throw new Error('변환할 수 없는 날짜입니다.'); } return newDate; };