Skip to content

Commit

Permalink
feat: (#55) 옵션에 따라 마감 시간을 가공하여 반환하는 함수 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
inyeong-kang committed Jul 17, 2023
1 parent dba88a2 commit 168452a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions frontend/src/types/post.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { WritingVoteOptionType } from '@hooks/useWritingOption';

export interface WrittenVoteOptionType {
id: number;
text: string;
Expand All @@ -20,3 +22,11 @@ export interface PostInfo {
options: WrittenVoteOptionType[];
};
}

export interface PostRequest {
categoryIds: number[];
title: string;
content: string;
postOptions: WritingVoteOptionType[];
deadline?: string;
}
33 changes: 33 additions & 0 deletions frontend/src/utils/formatTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
interface Time {
day: number;
hour: number;
minute: number;
}

export function addTimeToCurrentDate({ day, hour, minute }: Time) {
// 현재 시간을 가져옵니다.
const now = new Date();

// 입력된 값들을 더하여 새로운 시간을 계산합니다.
const newTime = new Date(now);
newTime.setDate(now.getDate() + day);
newTime.setHours(now.getHours() + hour);
newTime.setMinutes(now.getMinutes() + minute);

// 'yyyy-dd-mm hh:mm' 형식으로 변환하여 반환합니다.
const newYear = newTime.getFullYear();
const newDay = String(newTime.getDate()).padStart(2, '0');
const newMonth = String(newTime.getMonth() + 1).padStart(2, '0');
const newHour = String(newTime.getHours()).padStart(2, '0');
const newMinute = String(newTime.getMinutes()).padStart(2, '0');

return `${newYear}-${newMonth}-${newDay} ${newHour}:${newMinute}`;
}

export function formatTimeWithOption(option: string) {
if (option === '10분') return { day: 0, hour: 0, minute: 10 };
else if (option === '30분') return { day: 0, hour: 0, minute: 30 };
else if (option === '1시간') return { day: 0, hour: 1, minute: 0 };
else if (option === '6시간') return { day: 0, hour: 6, minute: 0 };
else return { day: 1, hour: 0, minute: 0 };
}

0 comments on commit 168452a

Please sign in to comment.