# Calculate the number of days between two dates

Count whole calendar days between two YYYY-MM-DD dates.

Canonical: https://1loc.dev/date-time/calculate-the-number-of-difference-days-between-two-dates/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function diffDays({ first, second }) { return Math.round(Math.abs(Date.parse(second) - Date.parse(first)) / 86400000); }
```

## Input

```json
{
  "first": "2014-12-19",
  "second": "2020-01-01"
}
```

## Expected output

```json
1839
```

## Edge case

Input:
```json
{
  "first": "2026-10-28",
  "second": "2026-10-28"
}
```
Output:
```json
0
```

## Limits

- Both dates are UTC calendar days, so daylight-saving changes cannot add or lose an hour. The original rounded local Date differences up with Math.ceil.
- The result is absolute: the order of the dates does not change it.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/calculate-the-number-of-difference-days-between-two-dates.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Uses validated UTC calendar dates and Math.round instead of Math.ceil on local Date objects, avoiding daylight-saving errors. Original MDX retained as inert source evidence.
