두 개의 입력된 날짜끼리 비교하는 함수를 정리해놔요
두개 날짜 사이의 일수
1 2 3 4 5 6 7 8 9 10 11 12 13
| const getNumDaysBetweenDates = (startDate, endDate) => { let dates = []; const date = new Date(startDate); while (date < endDate) { dates = [...dates, new Date(date)] date.setDate(date.getDate() + 1) } return dates.length; }
const start = new Date(2021, 1, 31); const end = new Date(2021, 2, 2); const result = getNumDaysBetweenDates(start, end);
|
날짜가 더 먼저인지
1 2 3 4 5 6 7
| const isBefore = (date1, date2) => { return (date1.setHours(0, 0, 0, 0) - date2.setHours(0, 0, 0, 0)) < 0 }
const start = new Date(2020, 2, 3); const end = new Date(2021, 1, 2); const result = isBefore(start, end);
|
같은 날짜인지
1 2 3 4 5 6 7 8 9
| const isSameDay = (date1, date2) => { return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate(); } const start = new Date(2021, 3, 1); const end = new Date(2021, 3, 1); const result = isSameDay(end, start);
|
참고포스트