New to Digit.js?

Check out the getting started guide

Functions


toWord(number, type, decimalPrecision)

Takes in numbers and returns with commas or in word format.

It can take numbers up to Novemvigintillion, after which returns the inputed number but with commas.

Numbers under seven charecters will be returned with commas, but without other formatting.


The toWord function takes in two parameters, number and type. Type is optional, it defaults to long

number takes in a number value

type takes one value, "short", which will return short numbers, like t instead of trillion.

decimalPrecision is an optional value, it takes in a whole number from 0 to 3, then determines how many decimial points of precision are in the result. It applies to numbers less than one million. It will not fill in empty spots.


Usage

// Returns "1 B"
   console.log(toWord(1000000000, "short"));

formatDate(date, format)

Takes in a date object and returns it in a specified format.

It supports d/m/y, m/d/y, and a special format - Short month day full year, like Mar 30 2023.


The formatDate function takes in two parameters, date and format.

date accepts a date object

format takes in a string, "dmy", "mdy, or "quick". If nothing is inpputed for format, it defaults to .toLocaleString()


Usage

// Returns "30/3/23" (for today anyway :)
   console.log(formatDate(new Date(), "dmy"));

formatTime(date, format)

Takes in a date object and returns the time in a specified format.

It supports 12 and 24 hour time, defaulting to 24.


The formatTime function takes in two parameters, date and format.

date accepts a date object

format takes in a string, "12" or nothing if you want 24 hour time.


Usage

// Returns "15:57" (for today anyway :)
   console.log(formatTime(new Date()));

timeBetween(date, date2)

Shows time difference between two dates, and if there's no second date, it defaults to now (your now, in my future)

It prints it out in a compact readable format, like "7 years" or "2 hours". If you want another format option, add an issue to the repository.


The timeBetween function takes in two parameters, date and date2.

date accepts a date object

date2 also accepts a date object, but it will default to the present if you leave it empty.


Usage

// Returns "about 3 months"
   console.log(timeBetween(new Date("12/2/22")));

formatTimeAmount(timeMs, units, decimalPrecision)

Takes in an amount of time in milliseconds and returns a string with the formatted time in the specified units.

It supports up to years, including months, weeks, days, hours, minutes, and seconds.


The formatTimeAmount function takes in three parameters, timeMs, untis, and decimalPrecision.

timeMs takes in the amount of time in milliseconds.

units takes in a string with the units to include, with y for years, M for months, w for weeks, d for days, h for hours, m for minutes, and s for seconds.

decimalPrecision takes in the number of decimal points to include for the seconds, in the range 1-3.


Usage

// Returns "73 years 3 weeks 6 days 14 hours 23 minutes and 45 seconds"
   console.log(formatTimeAmount(2304253425432));


Library reserved words

Reserved keywords: toWord, formatDate, formatTime, dateIs, timeIs, timeBetween,



Notes?

Eventually, I'll dynamically generate the names for the numbers, but for now it's manual.

Thanks for coming :)