230831 javascript Intl을 사용하여 날짜 시간 다루기
채팅방에서 채팅에 대한 각각의 시간과 일자별로 날짜를 알려주기 위해서 Intl 객체를 사용하여 사용자의 언어에 맞게 시간을 계산하여 보여줄 수 있다.
시간 보여 주기
const getTimeText = (isoDateString: string) => {
const isoDate = new Date(isoDateString);
const options: Intl.DateTimeFormatOptions = { hour: 'numeric', minute: 'numeric' };
return new Intl.DateTimeFormat(navigator.language, options).format(isoDate);
};
getTimeText('2023-08-30 10:12:05.287692+00') // "오후 7:12"
날짜 보여주기
const getDateText = (isoDateString: string): string => {
const isoDate = new Date(isoDateString);
const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' };
return new Intl.DateTimeFormat(navigator.language, options).format(isoDate);
};
getDateText('2023-08-30 10:12:05.287692+00') // "2023년 8월 30일"
'내일배움캠프' 카테고리의 다른 글
230903 내일배움캠프 16주차 WIL (0) | 2023.09.03 |
---|---|
230901 nextjs 서버컴포넌트에서 form 사용하기 (0) | 2023.09.01 |
230830 react router dom에서 useSearchParams로 쿼리 스트링 조작하기 (0) | 2023.08.30 |
230829 supabase inner join을 통해 필요한 데이터 한 번에 가져오기 (0) | 2023.08.29 |
230828 sendbird 용량 초과로 인한 supabase로의 chat 구현 (0) | 2023.08.28 |