DEV Community

wes5510
wes5510

Posted on

2 2

모바일에서 invalid date

TL;DR

  • date는 string X, number O

본론

배경

데스크탑 웹에서는 잘 동작하는 date가 모바일에서는 Invaild date 오류를 뿜음
해결 과정

구글링을 해보니 이런 게 있었음 RangeError: invalid date
오늘의 연, 월, 일을 가져오는 함수에서 yyyy.mm.dd 형식의 string을 date로 만들어주는 게 문제가 됨.

const getYearMonthDate = (date: Date): Date =>
    new Date(`${date.getFullYear()}.${date.getMonth() + 1}.${date.getDate()}`);
Enter fullscreen mode Exit fullscreen mode

setHours(0, 0, 0, 0)으로 string을 안쓰고 해결함

const getYearMonthDate = (date: Date): Date =>
    new Date(date.setHours(0, 0, 0, 0));
Enter fullscreen mode Exit fullscreen mode

결론

new Date 인자값으로는 number를 써야하고 다른 언어와 연동되기 쉽게 DB에는 number(Unix Time Stamp)로 DB에 저장하는 게 좋다.

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series