Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Google Refund
- Google Developer API
- Unity Editor
- react
- express
- Git
- spread 연산자
- critical rendering path
- Camera Zoom
- Spring Boot
- Digital Ocean
- docker
- Unity IAP
- rpg server
- java
- mongoDB
- css framework
- OverTheWire
- SDK upgrade
- springboot
- server
- Camera Movement
- Packet Network
- linux
- --watch
- screencapture
- MySQL
- nodejs
- unity
- draganddrop
Archives
- Today
- Total
우당탕탕 개발일지
[Javascript] dayjs 를 이용하여 문자열을 Date로 파싱하기 본문
날짜를 표기하는 여러 포맷들이 있다.
YYYY-MM-DD 나, YYYY년 MM월 DD일 등등.. 이런 다양한 날짜 포맷을 Date 형식으로 변환하는 작업이 필요했다. 이때 알게 된 것이 dayjs이다.
프로젝트에 dayjs를 설치한다.
npm install dayjs
커스텀 날짜 포맷을 적용하여 Date로 변환하기
import dayjs from 'dayjs';
dayjs.extend(customParseFormat); //여러가지 커스텀 포맷을 지원하기 위해 확장
//변환포맷을 커스텀으로 여러개 설정해놓는다.
date_formats = [
'YYYY년MM월DD일',
'YYYY년M월D일',
'YYYY/MM/DD',
'YYYY-MM-DD',
'YYYY.MM.DD hh:mm:ss',
'YYYY.MM.DD',
];
parseStrToDate(input: string): Date {
for (const format of date_formats) {
const date = dayjs(input, format);
if (date.isValid()) {
return date.toDate();
}
}
console.log(`Invalid date format: ${input}`);
return new Date();
}
'Client' 카테고리의 다른 글
전투시스템 설계 - StatManager 로 다양한 효과 전투에 적용하기 (0) | 2025.02.12 |
---|---|
[JavaScript] 얕은복사 vs 깊은복사 (0) | 2024.08.11 |
Web 기초 (0) | 2024.08.01 |
[React] react-beautiful-dnd : Drag and Drop 구현하기 (0) | 2024.05.21 |
[React] CSS Framework : TailWind CSS 사용하기 (0) | 2024.05.21 |