quick-ms
TypeScript icon, indicating that this package has built-in type declarations

1.2.5 • Public • Published

QUICK-MS

Basically that's an old, well known "ms" - but in it's advanced form.

⚔️ quick-ms VS. ms

  • It is generally slightly faster
  • Shows back real values, or really close to them (Classic ms return just rounded values)
  • Supports more time formats (text, 24h & 12h types)
  • Includes @Types (It is written in TypeScript)

⚙️ Features

  • Works both in Node.js and in the browser
  • Returns TypeError each time when u pass wrong argument.
  • If you pass a string with a number and a valid unit, the number of equivalent milliseconds is returned

📚 Functions/Mechanics

Function Arguments Description
getMilliseconds timeFormat: string Returns a total number of milliseconds.
getTimeObject ms: number Calculates "timeObject" from raw amount of milliseconds.
getAccurateTimeObject ms: number Works in the same way as getTimeObject() but uses more accurate values.
getReadableTime ms: number, options: readableOptions Return raw time value as a human readable string.

❔ What is "readableOptions"

Option Type Default value Description
option.compact Boolean false Displays human readable time in minimal version.
option.showEmpty Boolean false If true - It gonna display also fields where value = 0.
Example: "1d, 13min" => "1d, 0hrs, 13min"
option.showMS Boolean false If true - It will display also number of milliseconds if there are any.

Examples

  • Getting raw values (milliseconds) :
import { getMilliseconds } from 'quick-ms';

console.log(getMilliseconds('1 day, 12 minutes'));
// OUT: 87120000

console.log(getMilliseconds('1h'));
console.log(getMilliseconds('-01:00'));
// OUT[1]: 3600000
// OUT[2]: 3600000

console.log(getMilliseconds('09:00 AM'));
// OUT: 32400000

console.log(getMilliseconds('4 years'));
// OUT: 126160000000

console.log(getMilliseconds('3 months, 8 weeks'));
// OUT: 12614400000

console.log(getMilliseconds('10mo10m'));
// OUT: 25920600000

❗️ Remember

Format: XX:XX stands for Hh:Mm, if you want to be more accurate - you can add information about seconds. - XX:XX:XX that stands for Hh:Mm:Ss


  • Displaying raw ms into readable time:
import { getMilliseconds, getReadableTime } from 'quick-ms';

console.log(getReadableTime(1000));
// OUT: 1 second

console.log(getReadableTime(new Date().getTime()));
// OUT: 51 years, 5 hours, 15 minutes, 41 seconds

console.log(getReadableTime(23233000, { compact: true }));
// OUT: 6hrs, 27min, 13sec

console.log(getReadableTime(new Date(2014, 0, 1, 10, 40).getTime() - new Date(2014, 0, 1, 10, 5).getTime()));
// OUT: 35 minutes
  • Do you need higher accuracy?
import { getTimeObject, getAccurateTimeObject } from 'quick-ms';

// For most users that's enough:
getTimeObject(Date.now());
// {
//   years: 51,
//   months: 0,
//   days: 0,
//   hours: 5,
//   minutes: 6,
//   seconds: 10,
//   milliseconds: 589
// }

// Grab accurate time object:
getAccurateTimeObject(Date.now());
// {
//   centuries: 0,
//   decades: 5,
//   calendarYears: 0,
//   months: 11,
//   weeks: 1,
//   days: 1,
//   hours: 14,
//   minutes: 52,
//   seconds: 50,
//   milliseconds: 587
// }

Why they have different values

Classic time object uses simplified numbers for months & years. It counts 52 weeks as a full year (instead 52.1429), 1 month as 30 days (instead 30.4167), etc.
getAccurateTimeObject() makes calculations on much more accurate numbers that are not rounded. Classic, rounded time object is good enough for 90% things.

Package Sidebar

Install

npm i quick-ms

Weekly Downloads

12

Version

1.2.5

License

ISC

Unpacked Size

51.1 kB

Total Files

15

Last publish

Collaborators

  • znudzony