What is pretty-time?
The pretty-time npm package is designed to format and display time durations in a human-readable format. It is particularly useful for converting raw time measurements in milliseconds into a more digestible format, which can be helpful in logging, monitoring, or displaying elapsed time in applications.
What are pretty-time's main functionalities?
Convert milliseconds to human-readable format
This feature allows developers to measure the duration of operations and display it in a format that is easy to read. The example shows how to use pretty-time to measure the time taken by an operation and print it in a human-readable format.
const prettyTime = require('pretty-time');
// Start a timer
const start = process.hrtime();
// Perform some operations...
// End the timer
const end = process.hrtime(start);
// Convert process time to a human-readable string
console.log('Operation took: ' + prettyTime(end));
Other packages similar to pretty-time
moment
Moment.js is a comprehensive date handling library that can also format durations. Unlike pretty-time, which focuses solely on formatting durations, Moment.js offers extensive capabilities for date and time manipulation, making it more versatile if your project requires handling of dates along with formatting durations.
dayjs
Day.js is a lightweight alternative to Moment.js that includes plugins for duration handling. It is similar to pretty-time in its simplicity and focus on minimalism but extends its utility to date manipulation as well.
ms
The ms package is another simple utility for converting various time formats to milliseconds and vice versa. It is similar to pretty-time in its simplicity and focus on time conversion, but it does not provide the same level of human-readable formatting.
pretty-time
Easily format the time from node.js process.hrtime
. Works with timescales ranging from weeks to nanoseconds.
Install
Install with npm
$ npm i pretty-time --save
Usage
var pretty = require('pretty-time');
var start = process.hrtime();
var time = process.hrtime(start);
console.log(pretty(time));
API
By default, when no time increment is given as the second argument, the closest timescale is used (e.g. most granular without being less than zero).
Examples:
pretty([1200708, 795428088]);
pretty([800708, 795428088]);
pretty([400708, 795428088]);
pretty([70708, 795428088]);
pretty([12708, 795428088]);
pretty([3708, 795428088]);
pretty([208, 795428088]);
pretty([20, 795428088]);
pretty([0, 795428088]);
pretty([0, 000428088]);
pretty([0, 000000088]);
pretty([0, 000000018]);
Minimum time increment
(All of the following examples use [6740, 795428088]
as the hrtime array.)
This value is passed as the second argument and determines how granular to make the time.
Examples
pretty(time, 'h');
pretty(time, 'm');
pretty(time, 's');
Valid time increments
Any of the following may be used:
ns
| nano
| nanosecond
| nanoseconds
μs
| micro
| microsecond
| microseconds
ms
| milli
| millisecond
| milliseconds
s
| sec
| second
| seconds
m
| min
| minute
| minutes
h
| hr
| hour
| hours
d
| day
| days
w
| wk
| week
| weeks
Related projects
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Author
Jon Schlinkert
License
Copyright © 2015 Jon Schlinkert
Released under the MIT license.
This file was generated by verb-cli on October 12, 2015.