{"id":13494977,"url":"https://github.com/magiclen/ts-date-differencer","last_synced_at":"2025-03-18T09:15:48.797Z","repository":{"id":64978118,"uuid":"580269056","full_name":"magiclen/ts-date-differencer","owner":"magiclen","description":"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.","archived":false,"fork":false,"pushed_at":"2023-10-30T13:36:53.000Z","size":483,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-03T04:47:52.096Z","etag":null,"topics":["date-differencer","datediff","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magiclen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-12-20T06:16:30.000Z","updated_at":"2023-03-06T18:14:44.000Z","dependencies_parsed_at":"2024-01-16T09:53:19.095Z","dependency_job_id":"0fa6cbc1-3e56-4b1f-ad12-c689900629e3","html_url":"https://github.com/magiclen/ts-date-differencer","commit_stats":{"total_commits":26,"total_committers":1,"mean_commits":26.0,"dds":0.0,"last_synced_commit":"8ee52aae9ff007bcae7847258b3896a9efa7d88c"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fts-date-differencer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fts-date-differencer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fts-date-differencer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fts-date-differencer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/ts-date-differencer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244189826,"owners_count":20412991,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["date-differencer","datediff","typescript"],"created_at":"2024-07-31T19:01:30.036Z","updated_at":"2025-03-18T09:15:48.774Z","avatar_url":"https://github.com/magiclen.png","language":"TypeScript","readme":"date-differencer\n==========\n\n[![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)\n\nCalculate 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.\n\n## Usage\n\n```typescript\nimport {\n    dateDiff, dateTimeDiff, dayDiff, dayTimeDiff,\n    addDateTimeDiff, addDayTimeDiff\n} from \"date-differencer\";\n\nconst a = new Date(2022, 5, 6, 0);\nconst b = new Date(2023, 7, 9, 1);\n\nconsole.log(dateDiff(a, b));\n/*\n{\n    \"years\": 1,\n    \"months\": 2,\n    \"days\": 3\n}\n*/\n\nconsole.log(dateTimeDiff(a, b));\n/*\n{\n    \"years\": 1,\n    \"months\": 2,\n    \"days\": 3,\n    \"hours\": 1,\n    \"minutes\": 0,\n    \"seconds\": 0,\n    \"milliseconds\": 0\n}\n*/\n\nconsole.log(Math.trunc(dayDiff(a, b))); // (365 + 31 + 30 + 3) = 429\n\nconsole.log(dayTimeDiff(a, b));\n/*\n{\n    \"days\": 429,\n    \"hours\": 1,\n    \"minutes\": 0,\n    \"seconds\": 0,\n    \"milliseconds\": 0\n}\n*/\n\nconsole.log(addDateTimeDiff(a, dateTimeDiff(a, b))); // the same as b\nconsole.log(addDayTimeDiff(a, dayTimeDiff(a, b)));   // the same as b\n```\n\nThis 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.\n\n```typescript\nimport { dateDiff } from \"date-differencer\";\n\nconst a = new Date(\"2020-02-27\");\nconst b = new Date(\"2021-03-01\");\n\nconsole.log(dateDiff(a, b));\n/*\n{\n    \"years\": 1,\n    \"months\": 0,\n    \"days\": 2\n}\n\nExplanation:\n    1. 2020-02-27 + 1 year -\u003e 2021-02-27\n    2. 2021-02-27 + 2 days -\u003e 2021-03-01 (2021-02 has 28 days)\n*/\n\nconsole.log(dateDiff(b, a));\n/*\n{\n    \"years\": -1,\n    \"months\": 0,\n    \"days\": -3\n}\n\nExplanation:\n    1. 2021-03-01 - 1 year -\u003e 2020-03-01\n    2. 2020-03-01 - 3 days -\u003e 2020-02-27 (2020-02 has 29 days)\n*/\n```\n\n## Usage for Browsers\n\n[Source](demo.html)\n\n[Demo Page](https://rawcdn.githack.com/magiclen/ts-date-differencer/master/demo.html)\n\n## License\n\n[MIT](LICENSE)","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fts-date-differencer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Fts-date-differencer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fts-date-differencer/lists"}