pretty-ms
Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`
Top Related Projects
Parse, validate, manipulate, and display dates in javascript.
⏳ Modern JavaScript date utility library ⌛️
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Tiny millisecond conversion utility
A tiny (349B) reusable date formatter. Extremely fast!
Quick Overview
Pretty-ms is a lightweight JavaScript library that converts milliseconds to a human-readable string format. It provides a simple and flexible way to represent time durations in a more readable manner, making it useful for various applications, including logging, user interfaces, and time-based calculations.
Pros
- Easy to use with a simple API
- Highly customizable output format
- Supports both Node.js and browser environments
- Actively maintained with regular updates
Cons
- Limited to millisecond-based input
- May require additional formatting for complex time representations
- Doesn't support localization out of the box
Code Examples
Converting milliseconds to a human-readable string:
import prettyMilliseconds from 'pretty-ms';
console.log(prettyMilliseconds(1337000000));
//=> '15d 11h 23m 20s'
Using compact option for shorter output:
import prettyMilliseconds from 'pretty-ms';
console.log(prettyMilliseconds(1337, {compact: true}));
//=> '1.3s'
Customizing units and separators:
import prettyMilliseconds from 'pretty-ms';
console.log(prettyMilliseconds(1337, {
separateMilliseconds: true,
formatSubMilliseconds: true,
separatorString: ' | '
}));
//=> '1s | 337ms'
Getting Started
To use pretty-ms in your project, follow these steps:
-
Install the package using npm:
npm install pretty-ms
-
Import and use the function in your JavaScript code:
import prettyMilliseconds from 'pretty-ms'; const duration = prettyMilliseconds(1234567890); console.log(duration); //=> '14d 6h 56m 7s'
That's it! You can now start using pretty-ms to convert milliseconds to human-readable strings in your project.
Competitor Comparisons
Parse, validate, manipulate, and display dates in javascript.
Pros of moment
- Comprehensive date and time manipulation library with extensive features
- Supports parsing, validating, manipulating, and formatting dates
- Well-established with a large community and extensive documentation
Cons of moment
- Larger bundle size, which can impact performance in browser environments
- More complex API due to its extensive feature set
- No longer recommended for new projects (in maintenance mode)
Code comparison
moment:
const duration = moment.duration(123456789);
console.log(duration.humanize()); // "1 day"
console.log(duration.asHours()); // 34.293552500000005
pretty-ms:
const prettyMs = require('pretty-ms');
console.log(prettyMs(123456789)); // "1d 10h 17m 36s 789ms"
console.log(prettyMs(123456789, {compact: true})); // "1d 10h"
Key differences
- pretty-ms focuses specifically on converting milliseconds to human-readable strings
- moment provides a more comprehensive date and time manipulation toolkit
- pretty-ms is lightweight and has a simpler API for its specific use case
- moment offers more flexibility in formatting and manipulating dates and times
Use cases
- Choose pretty-ms for simple millisecond to human-readable string conversion
- Use moment for complex date and time operations, parsing, and formatting
- Consider alternatives like date-fns or Luxon for new projects requiring comprehensive date/time functionality
⏳ Modern JavaScript date utility library ⌛️
Pros of date-fns
- Comprehensive date manipulation library with a wide range of functions
- Modular architecture allows for tree-shaking and smaller bundle sizes
- Supports multiple locales for internationalization
Cons of date-fns
- Larger package size compared to pretty-ms
- Steeper learning curve due to its extensive API
- May be overkill for simple time formatting tasks
Code Comparison
pretty-ms:
import prettyMs from 'pretty-ms';
console.log(prettyMs(1337000000)); // Output: 15d 11h 23m 20s
date-fns:
import { formatDuration, intervalToDuration } from 'date-fns';
const duration = intervalToDuration({ start: 0, end: 1337000000 });
console.log(formatDuration(duration)); // Output: 15 days 11 hours 23 minutes 20 seconds
pretty-ms is a lightweight library focused on converting milliseconds to human-readable strings, while date-fns is a more comprehensive date manipulation library. pretty-ms excels in simplicity and ease of use for basic time formatting tasks, whereas date-fns offers a broader range of date-related functions and more flexibility in formatting options. The choice between the two depends on the specific requirements of your project and the complexity of date operations needed.
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Pros of dayjs
- More comprehensive date and time manipulation library
- Supports parsing, validating, manipulating, and formatting dates
- Chainable API for more complex operations
Cons of dayjs
- Larger bundle size due to more features
- May be overkill for simple time formatting tasks
- Steeper learning curve for basic use cases
Code comparison
pretty-ms:
import prettyMs from 'pretty-ms';
console.log(prettyMs(1337000000));
// Output: 15d 11h 23m 20s
dayjs:
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
dayjs.extend(duration);
console.log(dayjs.duration(1337000000).format('D[d] H[h] m[m] s[s]'));
// Output: 15d 11h 23m 20s
Summary
pretty-ms is a lightweight library focused on converting milliseconds to human-readable strings, while dayjs is a more feature-rich date and time manipulation library. pretty-ms is simpler to use for basic time formatting tasks, but dayjs offers more flexibility for complex date and time operations. The choice between the two depends on the specific requirements of your project and the level of functionality needed.
Tiny millisecond conversion utility
Pros of ms
- Smaller package size and simpler implementation
- Supports both parsing and formatting of time strings
- Has a longer history and more widespread adoption
Cons of ms
- Less customization options for output formatting
- Doesn't support millisecond precision
- Limited localization support
Code Comparison
ms:
ms('2 days') // 172800000
ms('1d') // 86400000
ms(60000) // '1m'
pretty-ms:
prettyMs(1337000000) // '15d 11h 23m 20s'
prettyMs(1337) // '1.3s'
prettyMs(133) // '133ms'
Key Differences
- ms focuses on simplicity and bidirectional conversion between milliseconds and human-readable strings
- pretty-ms specializes in formatting milliseconds into more detailed, customizable human-readable strings
- ms uses shorthand notations (e.g., '2d' for 2 days), while pretty-ms provides more verbose output by default
- pretty-ms offers options for customizing output format, including unit display and precision
Use Cases
- Choose ms for simple time string parsing and basic formatting needs
- Opt for pretty-ms when more detailed and customizable time formatting is required, especially for displaying durations to end-users
A tiny (349B) reusable date formatter. Extremely fast!
Pros of tinydate
- Extremely lightweight (294 bytes minified + gzipped)
- Focused on date formatting with a simple, customizable API
- Faster performance for basic date formatting tasks
Cons of tinydate
- Limited functionality compared to pretty-ms (only handles date formatting)
- Doesn't support time duration formatting or parsing
- No built-in localization support
Code Comparison
tinydate:
import tinydate from 'tinydate';
const stamp = tinydate('{YYYY}-{MM}-{DD}');
console.log(stamp(new Date()));
pretty-ms:
import prettyMs from 'pretty-ms';
console.log(prettyMs(1337000000));
console.log(prettyMs(1337, { verbose: true }));
Summary
tinydate is a minimalist library focused on date formatting, offering a tiny footprint and fast performance. It's ideal for projects that only need basic date formatting functionality.
pretty-ms, on the other hand, is more versatile, specializing in converting milliseconds to human-readable strings. It offers more features like time duration formatting and verbose output options.
Choose tinydate for lightweight date formatting needs, and pretty-ms for more comprehensive time and duration handling requirements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
pretty-ms
Convert milliseconds to a human readable string:
1337000000
â15d 11h 23m 20s
Install
npm install pretty-ms
Usage
import prettyMilliseconds from 'pretty-ms';
prettyMilliseconds(1337000000);
//=> '15d 11h 23m 20s'
prettyMilliseconds(1337000000n);
//=> '15d 11h 23m 20s'
prettyMilliseconds(1337);
//=> '1.3s'
prettyMilliseconds(133);
//=> '133ms'
// `compact` option
prettyMilliseconds(1337, {compact: true});
//=> '1s'
// `verbose` option
prettyMilliseconds(1335669000, {verbose: true});
//=> '15 days 11 hours 1 minute 9 seconds'
// `colonNotation` option
prettyMilliseconds(95500, {colonNotation: true});
//=> '1:35.5'
// `formatSubMilliseconds` option
prettyMilliseconds(100.400080, {formatSubMilliseconds: true})
//=> '100ms 400µs 80ns'
// Can be useful for time durations
prettyMilliseconds(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))
//=> '35m'
API
prettyMilliseconds(milliseconds, options?)
milliseconds
Type: number | bigint
Milliseconds to humanize.
options
Type: object
secondsDecimalDigits
Type: number
Default: 1
Number of digits to appear after the seconds decimal point.
millisecondsDecimalDigits
Type: number
Default: 0
Number of digits to appear after the milliseconds decimal point.
Useful in combination with process.hrtime()
.
keepDecimalsOnWholeSeconds
Type: boolean
Default: false
Keep milliseconds on whole seconds: 13s
â 13.0s
.
Useful when you are showing a number of seconds spent on an operation and don't want the width of the output to change when hitting a whole number.
compact
Type: boolean
Default: false
Only show the first unit: 1h 10m
â 1h
.
Also ensures that millisecondsDecimalDigits
and secondsDecimalDigits
are both set to 0
.
unitCount
Type: number
Default: Infinity
Number of units to show. Setting compact
to true
overrides this option.
verbose
Type: boolean
Default: false
Use full-length units: 5h 1m 45s
â 5 hours 1 minute 45 seconds
separateMilliseconds
Type: boolean
Default: false
Show milliseconds separately. This means they won't be included in the decimal part of the seconds.
formatSubMilliseconds
Type: boolean
Default: false
Show microseconds and nanoseconds.
colonNotation
Type: boolean
Default: false
Display time using colon notation: 5h 1m 45s
â 5:01:45
. Always shows time in at least minutes: 1s
â 0:01
Useful when you want to display time without the time units, similar to a digital watch.
Setting colonNotation
to true
overrides the following options to false
:
compact
formatSubMilliseconds
separateMilliseconds
verbose
Related
- pretty-ms-cli - CLI for this module
- parse-ms - Parse milliseconds into an object
- to-milliseconds - Convert an object of time properties to milliseconds
- pretty-bytes - Convert bytes to a human readable string
Top Related Projects
Parse, validate, manipulate, and display dates in javascript.
⏳ Modern JavaScript date utility library ⌛️
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Tiny millisecond conversion utility
A tiny (349B) reusable date formatter. Extremely fast!
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot