Skip to content

Commit

Permalink
hotfix: 타이머에서 Date 객체를 사용했을 때 발생하는 에러 dayjs 라이브러리로 교체해서 해결 (#108)
Browse files Browse the repository at this point in the history
* fix: Date 객체를 dayjs 라이브러리로 교체
  • Loading branch information
corinthionia committed Jul 10, 2023
1 parent 1042c23 commit 2c14732
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/components/timer/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { Span, TextWrapper, Time, Wrapper } from './Timer.styles';
const Timer = ({ deadLine }: TimerTypes) => {
const [isTimeExpired, setIsTimeExpired] = useState<boolean>(false);

const targetDate = new Date(deadLine.replace(' ', 'T'));
const targetDate = dayjs(deadLine).format('YYYY-MM-DD HH:mm:00');
const { days, hours, minutes, seconds } = getCountdown(targetDate);

useEffect(() => {
if (!isTimeExpired) {
const now = dayjs(new Date());
const now = dayjs();
const end = dayjs(deadLine);

if (end.diff(now) < 1000) {
Expand Down
13 changes: 6 additions & 7 deletions src/utils/getCountdown.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import dayjs from 'dayjs';
import { useEffect, useState } from 'react';

export const getCountdown = (targetDate: Date) => {
const countDownDate = new Date(targetDate).getTime();
export const getCountdown = (targetDate: string) => {
const deadLine = dayjs(targetDate).format('YYYY-MM-DD HH:mm:00');

const [countDown, setCountDown] = useState(
countDownDate - new Date().getTime()
);
const [countDown, setCountDown] = useState(dayjs(deadLine).diff(dayjs()));

useEffect(() => {
setInterval(() => {
setCountDown(countDownDate - new Date().getTime());
setCountDown(dayjs(deadLine).diff(dayjs()));
}, 1000);
}, [countDownDate]);
}, [deadLine]);

return getReturnValues(countDown);
};
Expand Down

0 comments on commit 2c14732

Please sign in to comment.