Gathering detailed insights and metrics for itty-time
Gathering detailed insights and metrics for itty-time
Gathering detailed insights and metrics for itty-time
Gathering detailed insights and metrics for itty-time
An itty library to handle common time-related things for your API needs.
npm install itty-time
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
82 Stars
102 Commits
4 Forks
3 Watching
9 Branches
1 Contributors
Updated on 28 Nov 2024
TypeScript (84.16%)
JavaScript (15.84%)
Cumulative downloads
Total Downloads
Last day
13.4%
61,876
Compared to previous day
Last week
11.5%
340,712
Compared to previous week
Last month
586.5%
1,262,553
Compared to previous month
Last year
118,045.5%
1,459,097
Compared to previous year
Ultra-small (~390 bytes) library for TTL date math and converting ms durations to and from strings.
library | string to ms | ms to string | date math | size1 |
---|---|---|---|---|
itty-time | ✅ | ✅ | ✅ | 391b |
@lukeed/ms | ✅ | ✅ | ❌ | 435b |
ms | ✅ | ❌ | ❌ | 938b |
pretty-ms | ❌ | ✅ | ❌ | 1.04kB |
humanize-duration | ❌ | ✅ | ❌ | 6.74kB |
1: minified and gzipped
The only function most folks care about in terms of raw performance is string to ms conversion. In this, itty stacks up pretty well, being significantly faster than ms, but falling to the insanely-optimized @lukeed/ms.
Moral of the story, probably don't use ms.
Use Luke's if you want the absolute fastest parsing, or itty if you want some of the other functions as well. If you're byte-counting, itty wins again, but if you're byte-counting that hard, you're probably better off with raw ms math if you can stomach it.
seconds(duration: string) => number
ms(duration: string) => number
TTL math is a maintenance nightmare. It's a pain to write, a pain to read, and when you update the math later, you'll probably forget to update the comment, causing all sorts of mayhem.
1const TTL = 2 * 7 * 24 * 60 * 60 * 1000 // 2 weeks, right?
Here's a better way.
1import { ms, seconds } from 'itty-time' 2 3// to seconds 4seconds('2 weeks') // 1209600 5 6// to milliseconds 7ms('2 weeks') // 1209600000
duration(ms: number) => string
Of course, we sometimes need to go the other direction. Want to tell a user how long ago something happened? How much time they have left?
You could build it yourself, or import the fantastic humanize-duration library that inspired this, but at 6.3kB1, it's over 20x the size of this 280 byte function.
1: of course humanize-duration can also do much, much more.
1import { duration } from 'itty-time' 2 3duration(3750000) 4// "1 hour, 2 minutes, 30 seconds" 5 6// limit number of segments returned 7duration(3750000, { parts: 2 }) 8// "1 hour, 2.5 minutes" 9 10// change the delimiter 11duration(3750000, { join: ' --> ' }) 12// "1 hour --> 2 minutes --> 30 seconds" 13 14// or get the raw components 15duration(3750000, { join: false }) 16/* 17 [ 18 ['hour', 1], 19 ['minutes', 2], 20 ['seconds', 30] 21 ] 22/*
datePlus(duration: string, from = new Date) => Date
Sometimes you need a TTL for some point in the future, but sometimes you need the actual date. You could convert it all yourself... or use this.
1import { datePlus } from 'itty-time' 2 3// from right now 4datePlus('2 months') 5 6// or from a different date 7datePlus('2 months', datePlus('1 week'))
No vulnerabilities found.
No security vulnerabilities found.