Fix: Display 0 for expenses

This commit is contained in:
gpt-engineer-app[bot]
2025-04-05 06:02:31 +00:00
parent f8948e1221
commit e947a84dcb
4 changed files with 103 additions and 37 deletions

View File

@@ -7,17 +7,20 @@ import { ko } from 'date-fns/locale';
*/
export const parseTransactionDate = (dateStr: string): Date | null => {
// 빈 문자열 체크
if (!dateStr) {
if (!dateStr || dateStr === '') {
console.log('빈 날짜 문자열');
return null;
}
try {
// 특수 키워드 처리
if (dateStr.includes('오늘')) {
if (dateStr.toLowerCase().includes('오늘')) {
console.log('오늘 날짜로 변환');
return new Date();
}
if (dateStr.includes('어제')) {
if (dateStr.toLowerCase().includes('어제')) {
console.log('어제 날짜로 변환');
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return yesterday;
@@ -25,6 +28,7 @@ export const parseTransactionDate = (dateStr: string): Date | null => {
// ISO 형식 (yyyy-MM-dd) 시도
if (/^\d{4}-\d{2}-\d{2}/.test(dateStr)) {
console.log('ISO 형식 날짜 감지');
const date = parseISO(dateStr);
if (isValid(date)) {
return date;
@@ -36,6 +40,7 @@ export const parseTransactionDate = (dateStr: string): Date | null => {
const koreanMatch = dateStr.match(koreanDatePattern);
if (koreanMatch) {
console.log('한국어 날짜 형식 감지', koreanMatch);
const month = parseInt(koreanMatch[1]) - 1; // 0-based month
const day = parseInt(koreanMatch[2]);
const date = new Date();
@@ -44,6 +49,21 @@ export const parseTransactionDate = (dateStr: string): Date | null => {
return date;
}
// 쉼표 구분 형식 처리 (예: "오늘, 14:52 PM")
const commaSplit = dateStr.split(',');
if (commaSplit.length > 1) {
console.log('쉼표 구분 날짜 감지');
// 첫 부분이 오늘/어제 등의 키워드인 경우 처리
const firstPart = commaSplit[0].trim().toLowerCase();
if (firstPart === '오늘') {
return new Date();
} else if (firstPart === '어제') {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return yesterday;
}
}
// parse를 사용한 다양한 형식 시도
const formats = [
'yyyy-MM-dd',
@@ -51,13 +71,17 @@ export const parseTransactionDate = (dateStr: string): Date | null => {
'MM-dd-yyyy',
'MM/dd/yyyy',
'yyyy년 MM월 dd일',
'MM월 dd일'
'MM월 dd일',
'yyyy년 M월 d일',
'M월 d일'
];
for (const formatStr of formats) {
try {
console.log(`날짜 형식 시도: ${formatStr}`);
const date = parse(dateStr, formatStr, new Date(), { locale: ko });
if (isValid(date)) {
console.log('날짜 파싱 성공:', formatStr);
return date;
}
} catch (e) {
@@ -69,6 +93,7 @@ export const parseTransactionDate = (dateStr: string): Date | null => {
// 위 모든 형식이 실패하면 마지막으로 Date 생성자 시도
const dateFromConstructor = new Date(dateStr);
if (isValid(dateFromConstructor)) {
console.log('Date 생성자로 파싱 성공');
return dateFromConstructor;
}