[프로그래머스 LV0_JS]날짜 비교하기

Hyoyoung Kim·2023년 5월 29일
0

프로그래머스 레벨0

목록 보기
24/28

날짜 비교하기

https://school.programmers.co.kr/learn/courses/30/lessons/181838

문제 설명

정수 배열 date1과 date2가 주어집니다. 두 배열은 각각 날짜를 나타내며 [year, month, day] 꼴로 주어집니다. 각 배열에서 year는 연도를, month는 월을, day는 날짜를 나타냅니다.

만약 date1이 date2보다 앞서는 날짜라면 1을, 아니면 0을 return 하는 solution 함수를 완성해 주세요.

입출력 예시

코드 _ 방법 1

function solution(date1, date2) {
    let [year1, month1, day1] = date1;
    let [year2, month2, day2] = date2;
    if(year1!==year2) return year1<year2? 1:0;
    if(month1!==month2) return month1<month2?1:0;
    if(day1!==day2) return day1< day2? 1:0;
    return 0;
}

console.log(solution([2021, 12, 28],[2021, 12, 29])); //1
console.log(solution([1024, 10, 24],[1024, 10, 24])); //0
console.log(solution([2023, 1, 31], [2022, 12, 1])); //0
console.log(solution([2023, 5, 1], [2022, 4, 29])); //0

코드 _ 방법2

function solution(date1, date2) {
    return new Date(date1)<new Date(date2)? 1: 0;
}

console.log(solution([2021, 12, 28],[2021, 12, 29])); //1
console.log(solution([1024, 10, 24],[1024, 10, 24])); //0
console.log(solution([2023, 1, 31], [2022, 12, 1])); //0
console.log(solution([2023, 5, 1], [2022, 4, 29])); //0

0개의 댓글