coding test/leetCode
[leetCode/JS] 1360. Number of Days Between Two Dates
쭘봉
2022. 11. 23. 11:31
문제 설명
Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD
as shown in the examples.
입출력 예
Example 1:
Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1
Example 2:
Input: date1 = "2020-01-15", date2 = "2019-12-31"
Output: 15
Constraints
- The given dates are valid dates between the years
1971
and2100
.
내 솔루션
- 차이 나는 일자를 리턴하는 간단한 함수.
var daysBetweenDates = function(date1, date2) {
const time = new Date(date2) - new Date(date1);
return Math.abs(time / 1000 / 60 / 60 / 24)
};
감상평
- 쉬웠다. 하지만 많이 쓰이는 함수