├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.d.ts ├── index.test-d.ts ├── readme.md ├── package.json ├── license ├── index.js └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type TimeComponents = { 2 | days: T; 3 | hours: T; 4 | minutes: T; 5 | seconds: T; 6 | milliseconds: T; 7 | microseconds: T; 8 | nanoseconds: T; 9 | }; 10 | 11 | /** 12 | Parse milliseconds into an object. 13 | 14 | @example 15 | ``` 16 | import parseMilliseconds from 'parse-ms'; 17 | 18 | parseMilliseconds(1337000001); 19 | // { 20 | // days: 15, 21 | // hours: 11, 22 | // minutes: 23, 23 | // seconds: 20, 24 | // milliseconds: 1, 25 | // microseconds: 0, 26 | // nanoseconds: 0 27 | // } 28 | ``` 29 | */ 30 | export default function parseMilliseconds(milliseconds: T): TimeComponents; 31 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import parseMilliseconds, {type TimeComponents} from './index.js'; 3 | 4 | const result1: TimeComponents = parseMilliseconds(3000); 5 | 6 | expectType(result1.days); 7 | expectType(result1.hours); 8 | expectType(result1.minutes); 9 | expectType(result1.seconds); 10 | expectType(result1.milliseconds); 11 | expectType(result1.microseconds); 12 | expectType(result1.nanoseconds); 13 | 14 | const result2: TimeComponents = parseMilliseconds(3000n); 15 | 16 | expectType(result2.days); 17 | expectType(result2.hours); 18 | expectType(result2.minutes); 19 | expectType(result2.seconds); 20 | expectType(result2.milliseconds); 21 | expectType(result2.microseconds); 22 | expectType(result2.nanoseconds); 23 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # parse-ms 2 | 3 | > Parse milliseconds into an object 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install parse-ms 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import parseMilliseconds from 'parse-ms'; 15 | 16 | parseMilliseconds(1337000001); 17 | /* 18 | { 19 | days: 15, 20 | hours: 11, 21 | minutes: 23, 22 | seconds: 20, 23 | milliseconds: 1, 24 | microseconds: 0, 25 | nanoseconds: 0 26 | } 27 | */ 28 | 29 | parseMilliseconds(1337000001n); 30 | /* 31 | { 32 | days: 15n, 33 | hours: 11n, 34 | minutes: 23n, 35 | seconds: 20n, 36 | milliseconds: 1n, 37 | microseconds: 0n, 38 | nanoseconds: 0n 39 | } 40 | */ 41 | ``` 42 | 43 | ## Related 44 | 45 | - [to-milliseconds](https://github.com/sindresorhus/to-milliseconds) - The inverse of this module 46 | - [pretty-ms](https://github.com/sindresorhus/pretty-ms) - Convert milliseconds to a human readable string 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "parse-ms", 3 | "version": "4.0.0", 4 | "description": "Parse milliseconds into an object", 5 | "license": "MIT", 6 | "repository": "sindresorhus/parse-ms", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "browser", 31 | "parse", 32 | "time", 33 | "ms", 34 | "milliseconds", 35 | "microseconds", 36 | "nanoseconds", 37 | "duration", 38 | "period", 39 | "range", 40 | "interval" 41 | ], 42 | "devDependencies": { 43 | "ava": "^6.0.1", 44 | "tsd": "^0.31.0", 45 | "xo": "^0.58.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const toZeroIfInfinity = value => Number.isFinite(value) ? value : 0; 2 | 3 | function parseNumber(milliseconds) { 4 | return { 5 | days: Math.trunc(milliseconds / 86_400_000), 6 | hours: Math.trunc(milliseconds / 3_600_000 % 24), 7 | minutes: Math.trunc(milliseconds / 60_000 % 60), 8 | seconds: Math.trunc(milliseconds / 1000 % 60), 9 | milliseconds: Math.trunc(milliseconds % 1000), 10 | microseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1000) % 1000), 11 | nanoseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1e6) % 1000), 12 | }; 13 | } 14 | 15 | function parseBigint(milliseconds) { 16 | return { 17 | days: milliseconds / 86_400_000n, 18 | hours: milliseconds / 3_600_000n % 24n, 19 | minutes: milliseconds / 60_000n % 60n, 20 | seconds: milliseconds / 1000n % 60n, 21 | milliseconds: milliseconds % 1000n, 22 | microseconds: 0n, 23 | nanoseconds: 0n, 24 | }; 25 | } 26 | 27 | export default function parseMilliseconds(milliseconds) { 28 | switch (typeof milliseconds) { 29 | case 'number': { 30 | if (Number.isFinite(milliseconds)) { 31 | return parseNumber(milliseconds); 32 | } 33 | 34 | break; 35 | } 36 | 37 | case 'bigint': { 38 | return parseBigint(milliseconds); 39 | } 40 | 41 | // No default 42 | } 43 | 44 | throw new TypeError('Expected a finite number or bigint'); 45 | } 46 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import parseMilliseconds from './index.js'; 3 | 4 | const toBigInt = milliseconds => { 5 | if (typeof milliseconds !== 'number') { 6 | return; 7 | } 8 | 9 | try { 10 | return BigInt(milliseconds); 11 | } catch {} 12 | }; 13 | 14 | /** 15 | @template {T extend number | bigint} 16 | @param {import('ava').Assertions} t 17 | @param {T} milliseconds 18 | @param {import('./index.js').TimeComponents} expected 19 | */ 20 | function runTest(t, milliseconds, expected) { 21 | t.deepEqual(parseMilliseconds(milliseconds), expected); 22 | 23 | const bigint = toBigInt(milliseconds); 24 | if (typeof bigint !== 'bigint') { 25 | return; 26 | } 27 | 28 | t.deepEqual( 29 | parseMilliseconds(bigint), 30 | Object.fromEntries( 31 | Object.entries(expected).map(([unit, value]) => [unit, BigInt(value)]), 32 | ), 33 | ); 34 | } 35 | 36 | test('parse milliseconds into an object', t => { 37 | runTest(t, 1000 + 400, { 38 | days: 0, 39 | hours: 0, 40 | minutes: 0, 41 | seconds: 1, 42 | milliseconds: 400, 43 | microseconds: 0, 44 | nanoseconds: 0, 45 | }); 46 | 47 | runTest(t, 1000 * 55, { 48 | days: 0, 49 | hours: 0, 50 | minutes: 0, 51 | seconds: 55, 52 | milliseconds: 0, 53 | microseconds: 0, 54 | nanoseconds: 0, 55 | }); 56 | 57 | runTest(t, 1000 * 67, { 58 | days: 0, 59 | hours: 0, 60 | minutes: 1, 61 | seconds: 7, 62 | milliseconds: 0, 63 | microseconds: 0, 64 | nanoseconds: 0, 65 | }); 66 | 67 | runTest(t, 1000 * 60 * 5, { 68 | days: 0, 69 | hours: 0, 70 | minutes: 5, 71 | seconds: 0, 72 | milliseconds: 0, 73 | microseconds: 0, 74 | nanoseconds: 0, 75 | }); 76 | 77 | runTest(t, 1000 * 60 * 67, { 78 | days: 0, 79 | hours: 1, 80 | minutes: 7, 81 | seconds: 0, 82 | milliseconds: 0, 83 | microseconds: 0, 84 | nanoseconds: 0, 85 | }); 86 | 87 | runTest(t, 1000 * 60 * 60 * 12, { 88 | days: 0, 89 | hours: 12, 90 | minutes: 0, 91 | seconds: 0, 92 | milliseconds: 0, 93 | microseconds: 0, 94 | nanoseconds: 0, 95 | }); 96 | 97 | runTest(t, 1000 * 60 * 60 * 40, { 98 | days: 1, 99 | hours: 16, 100 | minutes: 0, 101 | seconds: 0, 102 | milliseconds: 0, 103 | microseconds: 0, 104 | nanoseconds: 0, 105 | }); 106 | 107 | runTest(t, 1000 * 60 * 60 * 999, { 108 | days: 41, 109 | hours: 15, 110 | minutes: 0, 111 | seconds: 0, 112 | milliseconds: 0, 113 | microseconds: 0, 114 | nanoseconds: 0, 115 | }); 116 | 117 | runTest(t, (1000 * 60) + 500 + 0.345_678, { 118 | days: 0, 119 | hours: 0, 120 | minutes: 1, 121 | seconds: 0, 122 | milliseconds: 500, 123 | microseconds: 345, 124 | nanoseconds: 678, 125 | }); 126 | 127 | runTest(t, 0.000_543, { 128 | days: 0, 129 | hours: 0, 130 | minutes: 0, 131 | seconds: 0, 132 | milliseconds: 0, 133 | microseconds: 0, 134 | nanoseconds: 543, 135 | }); 136 | 137 | runTest( 138 | t, 139 | 0n 140 | // 1ms 141 | + 1n 142 | // 2s 143 | + (2n * 1000n) 144 | // 3m 145 | + (3n * 1000n * 60n) 146 | // 4h 147 | + (4n * 1000n * 60n * 60n) 148 | // Days 149 | + (BigInt(Number.MAX_VALUE) * 1000n * 60n * 60n * 24n), 150 | { 151 | days: BigInt(Number.MAX_VALUE), 152 | hours: 4n, 153 | minutes: 3n, 154 | seconds: 2n, 155 | milliseconds: 1n, 156 | microseconds: 0n, 157 | nanoseconds: 0n, 158 | }, 159 | ); 160 | 161 | t.deepEqual(parseMilliseconds(Number.MAX_VALUE), { 162 | days: 2.080_663_350_535_087_5e+300, 163 | hours: 8, 164 | minutes: 8, 165 | seconds: 48, 166 | milliseconds: 368, 167 | microseconds: 0, 168 | nanoseconds: 0, 169 | }); 170 | 171 | t.deepEqual(parseMilliseconds(Number.MIN_VALUE), { 172 | days: 0, 173 | hours: 0, 174 | minutes: 0, 175 | seconds: 0, 176 | milliseconds: 0, 177 | microseconds: 0, 178 | nanoseconds: 0, 179 | }); 180 | }); 181 | 182 | test('handle negative millisecond values', t => { 183 | const units = [ 184 | 'days', 185 | 'hours', 186 | 'minutes', 187 | 'seconds', 188 | 'milliseconds', 189 | ]; 190 | 191 | const times = [ 192 | 0.0005, 193 | 0.3, 194 | ...[ 195 | 100 + 400, 196 | 1000 * 55, 197 | 1000 * 67, 198 | 1000 * 60 * 5, 199 | 1000 * 60 * 67, 200 | 1000 * 60 * 60 * 12, 201 | 1000 * 60 * 60 * 40, 202 | 1000 * 60 * 60 * 999, 203 | ].flatMap(number => [number, toBigInt(number)]), 204 | BigInt('0x' + 'F'.repeat(1e6)), 205 | ]; 206 | 207 | for (const milliseconds of times) { 208 | const positive = parseMilliseconds(milliseconds); 209 | const negative = parseMilliseconds(-milliseconds); 210 | 211 | for (const unit of units) { 212 | t.is(negative[unit], -positive[unit]); 213 | } 214 | } 215 | }); 216 | 217 | test('errors', t => { 218 | t.throws(() => { 219 | parseMilliseconds('string'); 220 | }); 221 | 222 | t.throws(() => { 223 | parseMilliseconds(Number.NaN); 224 | }); 225 | 226 | t.throws(() => { 227 | parseMilliseconds(Number.POSITIVE_INFINITY); 228 | }); 229 | }); 230 | --------------------------------------------------------------------------------