code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

1360 Number Of Days Between Two Dates

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/*
 * 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
 */

function numberOfDaysBetweenTwoDates(date1, date2) {
  return Math.round(Math.abs(new Date(date2).getTime() - new Date(date1).getTime()) / (60 * 60 * 24 * 1000))
}

test('number Of Days Between Two Dates', () => {
  expect(numberOfDaysBetweenTwoDates('2019-06-29', '2019-06-30')).toEqual(1)
  expect(numberOfDaysBetweenTwoDates('2020-01-15', '2019-12-31')).toEqual(15)
})

Created 2020-04-18T23:14:05+00:00 · Edit