Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/magiclen/ts-date-differencer
Calculate the time interval between two `Date` objects and output the result in years plus months plus days plus hours plus minutes plus seconds plus milliseconds (instead of representing the same duration in different units). This library is useful for lifespan check and age calculation.
https://github.com/magiclen/ts-date-differencer
date-differencer datediff typescript
Last synced: 18 days ago
JSON representation
Calculate the time interval between two `Date` objects and output the result in years plus months plus days plus hours plus minutes plus seconds plus milliseconds (instead of representing the same duration in different units). This library is useful for lifespan check and age calculation.
- Host: GitHub
- URL: https://github.com/magiclen/ts-date-differencer
- Owner: magiclen
- License: mit
- Created: 2022-12-20T06:16:30.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-30T13:36:53.000Z (about 1 year ago)
- Last Synced: 2024-05-03T04:47:52.096Z (7 months ago)
- Topics: date-differencer, datediff, typescript
- Language: TypeScript
- Homepage:
- Size: 472 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- my-awesome-list - ts-date-differencer
README
date-differencer
==========[![CI](https://github.com/magiclen/ts-date-differencer/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/ts-date-differencer/actions/workflows/ci.yml)
Calculate the time interval between two `Date` objects and output the result in years plus months plus days plus hours plus minutes plus seconds plus milliseconds (instead of representing the same duration in different units). This library is useful for lifespan check and age calculation.
## Usage
```typescript
import {
dateDiff, dateTimeDiff, dayDiff, dayTimeDiff,
addDateTimeDiff, addDayTimeDiff
} from "date-differencer";const a = new Date(2022, 5, 6, 0);
const b = new Date(2023, 7, 9, 1);console.log(dateDiff(a, b));
/*
{
"years": 1,
"months": 2,
"days": 3
}
*/console.log(dateTimeDiff(a, b));
/*
{
"years": 1,
"months": 2,
"days": 3,
"hours": 1,
"minutes": 0,
"seconds": 0,
"milliseconds": 0
}
*/console.log(Math.trunc(dayDiff(a, b))); // (365 + 31 + 30 + 3) = 429
console.log(dayTimeDiff(a, b));
/*
{
"days": 429,
"hours": 1,
"minutes": 0,
"seconds": 0,
"milliseconds": 0
}
*/console.log(addDateTimeDiff(a, dateTimeDiff(a, b))); // the same as b
console.log(addDayTimeDiff(a, dayTimeDiff(a, b))); // the same as b
```This library can handle leap years and odd/even number of days in a month correctly. The result of following code is a bit confusing but reasonable.
```typescript
import { dateDiff } from "date-differencer";const a = new Date("2020-02-27");
const b = new Date("2021-03-01");console.log(dateDiff(a, b));
/*
{
"years": 1,
"months": 0,
"days": 2
}Explanation:
1. 2020-02-27 + 1 year -> 2021-02-27
2. 2021-02-27 + 2 days -> 2021-03-01 (2021-02 has 28 days)
*/console.log(dateDiff(b, a));
/*
{
"years": -1,
"months": 0,
"days": -3
}Explanation:
1. 2021-03-01 - 1 year -> 2020-03-01
2. 2020-03-01 - 3 days -> 2020-02-27 (2020-02 has 29 days)
*/
```## Usage for Browsers
[Source](demo.html)
[Demo Page](https://rawcdn.githack.com/magiclen/ts-date-differencer/master/demo.html)
## License
[MIT](LICENSE)