반응형
그냥 한번 짜놓으면은 편한 나만의 공통 DateUtil을 만들어 보았다 . 소스를 첨부하겠다.
export const formatDate = (date: Date): string => {
return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date
.getDate()
.toString()
.padStart(2, "0")}`;
};
const getDateAgo = (days: number): string => {
const date = new Date();
date.setDate(date.getDate() - days);
return formatDate(date);
};
const getWeeksAgo = (weeks: number): string => {
return getDateAgo(weeks * 7);
};
const getMonthsAgo = (months: number): string => {
const date = new Date();
date.setMonth(date.getMonth() - months);
return formatDate(date);
};
export const DateUtils = {
today: formatDate(new Date()),
// Days 1일전 ~ 7일전
oneDayAgo: getDateAgo(1),
twoDaysAgo: getDateAgo(2),
threeDaysAgo: getDateAgo(3),
fourDaysAgo: getDateAgo(4),
fiveDaysAgo: getDateAgo(5),
sixDaysAgo: getDateAgo(6),
sevenDaysAgo: getDateAgo(7),
// Weeks 1주전 ~ 4주전
oneWeekAgo: getWeeksAgo(1),
twoWeeksAgo: getWeeksAgo(2),
threeWeeksAgo: getWeeksAgo(3),
fourWeeksAgo: getWeeksAgo(4),
// Months 1달전 ~ 12달전
oneMonthAgo: getMonthsAgo(1),
twoMonthsAgo: getMonthsAgo(2),
threeMonthsAgo: getMonthsAgo(3),
fourMonthsAgo: getMonthsAgo(4),
fiveMonthsAgo: getMonthsAgo(5),
sixMonthsAgo: getMonthsAgo(6),
sevenMonthsAgo: getMonthsAgo(7),
eightMonthsAgo: getMonthsAgo(8),
nineMonthsAgo: getMonthsAgo(9),
tenMonthsAgo: getMonthsAgo(10),
elevenMonthsAgo: getMonthsAgo(11),
twelveMonthsAgo: getMonthsAgo(12),
};
사용방법
const [searchParams, setSearchParams] = useState({
asdfDtMeetFrom: DateUtils.threeMonthsAgo,
asdfDtMeetTo: DateUtils.today,
asdfDtInputFrom: DateUtils.threeMonthsAgo,
asdfDtInputTo: DateUtils.today,
});
3달전 ~ 당일

반응형
'JavaScript' 카테고리의 다른 글
| [JavaScript] Excel 다운로드 기능 (0) | 2025.04.21 |
|---|---|
| [JavaScript] 서버사이드 렌더링(SSR) 클라이언트사이드 렌더링(CSR)? (4) | 2024.09.02 |
| [JavaScript] ES6란? (0) | 2024.08.30 |
| [JavaScript] undefined와 null 차이 (0) | 2024.08.28 |
| [JavaScript] const, var , let 변수 선언 차이점 (0) | 2024.08.28 |