From 116e4195ca85bc5c5b9ed9bfe390ed6eb9817899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=9E=AC=EB=AF=BC?= <95125109+javor10@users.noreply.github.com> Date: Thu, 20 Feb 2025 06:10:34 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EB=82=A0=EC=A7=9C=EA=B4=80=EB=A0=A8=20U?= =?UTF-8?q?TC+9=EB=A1=9C=20=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dtos/moment.dto.js | 51 ++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/dtos/moment.dto.js b/src/dtos/moment.dto.js index 8c0904a..77d001f 100644 --- a/src/dtos/moment.dto.js +++ b/src/dtos/moment.dto.js @@ -1,3 +1,27 @@ +// YYYY-MM-DD 형식으로 변환하는 함수 (UTC+9 적용) +const formatDate = (date) => { + if (!date) return null; + const validDate = date instanceof Date ? date : new Date(date); // 문자열이면 Date 객체로 변환 + if (isNaN(validDate.getTime())) { + console.error("Invalid date detected:", date); + return null; + } + const KST = new Date(validDate.getTime() + 9 * 60 * 60 * 1000); // UTC+9 적용 + return KST.toISOString().split("T")[0]; // YYYY-MM-DD +}; + +// YYYY-MM-DDTHH:mm:ss.sssZ 형식으로 변환하는 함수 (UTC+9 적용) +const formatDateTime = (date) => { + if (!date) return null; + const validDate = date instanceof Date ? date : new Date(date); // 문자열이면 Date 객체로 변환 + if (isNaN(validDate.getTime())) { + console.error("Invalid date detected:", date); + return null; + } + const KST = new Date(validDate.getTime() + 9 * 60 * 60 * 1000); // UTC+9 적용 + return KST.toISOString(); // ISO 형식 유지 +}; + export const bodyToCreateMoment = (body) => { if (!body) { throw new Error("요청 body값이 없습니다."); // body값이 없는 경우 @@ -46,8 +70,8 @@ export const responseFromCreateMoment = (moment) => { title: moment.title, date: formatDate(moment.createdAt), // YYYY-MM-DD 형식의 새로운 필드 추가 plannerId: moment.plannerId ?? null, - createdAt: moment.createdAt, - updatedAt: moment.updatedAt, + createdAt: formatDateTime(moment.createdAt), + updatedAt: formatDateTime(moment.updatedAt), momentContents: moment.momentContents.map(content => ({ sortOrder: content.sortOrder, content: content.content, @@ -56,13 +80,6 @@ export const responseFromCreateMoment = (moment) => { }; }; -// 🔍 YYYY-MM-DD 형식으로 변환하는 함수 -const formatDate = (date) => { - if (!date) return null; // 예외 처리 - return date.toISOString().split("T")[0]; // "YYYY-MM-DDTHH:mm:ss.sssZ" → "YYYY-MM-DD" -}; - - // moment 수정 DTO export const bodyToUpdateMoment = (body) => { @@ -110,8 +127,8 @@ export const responseFromUpdateMoment = (moment) => { userId: moment.userId, title: moment.title, plannerId: moment.plannerId || null, - createdAt: moment.createdAt, - updatedAt: moment.updatedAt, + createdAt: formatDateTime(moment.createdAt), + updatedAt: formatDateTime(moment.updatedAt), momentContents: moment.momentContents.map(content => ({ sortOrder: content.sortOrder, content: content.content, @@ -166,8 +183,8 @@ export const responseFromMyMomentDetail = (moment) => { title: moment.title, date: formatDate(moment.createdAt), plannerId: moment.plannerId ?? null, - createdAt: moment.createdAt, - updatedAt: moment.updatedAt, + createdAt: formatDateTime(moment.createdAt), + updatedAt: formatDateTime(moment.updatedAt), momentContents: moment.momentContents.map(content => ({ sortOrder: content.sortOrder, content: content.content, @@ -184,8 +201,8 @@ export const responseFromFriendsMoments = (moments) => { momentId: moment.id, title: moment.title, status: moment.status, - createdAt: moment.createdAt, - updatedAt: moment.updatedAt, + createdAt: formatDateTime(moment.createdAt), + updatedAt: formatDateTime(moment.updatedAt), thumbnailUrl: moment.momentContents[0]?.url || null })); }; @@ -198,8 +215,8 @@ export const responseFromFriendMomentDetail = (moment) => { title: moment.title, status: moment.status, plannerId: moment.plannerId || null, - createdAt: moment.createdAt, - updatedAt: moment.updatedAt, + createdAt: formatDateTime(moment.createdAt), + updatedAt: formatDateTime(moment.updatedAt), momentContents: moment.momentContents.map(content => ({ sortOrder: content.sortOrder, content: content.content,