About
time-date-tools
is a powerful Node.js module that allows you to easily manage time and date. You can configure a lot of features to not have date and time issues anymore.
Features
Installation
$ npm install --save time-date-tools
Usage
Importation
import tdt from "time-date-tools";
const tdt = require("time-date-tools");
Time
Format Time
Class TimeSettings for the format:
Before call the format()
function, you have to create a class called TimeSettings
. This function allows you to save the configuration you want in a variable, instead of rewrite everything.
new TimeSettings(options);
Available options in the configuration for the format()
function:
Parameter | Type | Optional | Default | Description |
---|
lang | string | ✅ | en | Language of time unities ('en' or 'fr'). |
format | string | ✅ | Y YYYY, M MMMM, W WWWW, D dddd, h HH, m MM, s SS, sss SSSS | Format* of the string time. |
precision | boolean | ✅ | true | If false, it will round and return the bigger nonnull unity. Else, all the unities are returned. |
long | boolean | ✅ | true | Complete the previous argument, if false, it will return the short unity. Else it will return the long unity. |
*Format: By default, the null unities aren't displayed. If you want to display them, you can surround some parts of the format by [
and ]
.
Function format():
Now the settings ready, you can call the function format()
which convert a time in milliseconds to a string with the format of your choice.
const TimeSettingsFormat = new TimeSettings({ lang: "en", format: "Y YYYY, M MMMM, W WWWW, D dddd, h HH, m MM, s SS, sss SSSS" });
TimeSettingsFormat.format(timeInMilliseconds);
Available arguments in the format()
function:
Parameter | Type | Optional | Default | Description |
---|
timeInMilliseconds | number | ❌ | | Time in milliseconds to convert into a string. |
Examples:
const settings_1 = new TimeSettings({ lang: "en", format: "Y YYYY, M MMMM, W WWWW, D dddd, h HH, m MM, s SS, sss SSSS" });
const settings_2 = new TimeSettings({ lang: "en", format: "[Y YYYY, M MMMM,] W WWWW, D dddd, h HH, m MM, s SS, sss SSSS" });
settings_1.format(361410);
settings_2.format(361410);
const settings_3 = new TimeSettings({ lang: "en", precision: false, long: true });
const settings_4 = new TimeSettings({ lang: "en", precision: false, long: false });
const settings_5 = new TimeSettings({ lang: "en", precision: true, long: true });
settings_3.format(486000000);
settings_4.format(486000000);
settings_5.format(486000000);
Parse Time
Class TimeSettings for the parse:
Before call the parse()
function, you have to create a class called TimeSettings
. This function allows you to save the configuration you want in a variable, instead of rewrite everything.
new TimeSettings();
There is no available option for the parse()
function. You can put the same options as for the format()
function, but it won't change anything for the result.
Function parse():
Now the settings ready, you can call the function parse()
which convert a time in string to milliseconds.
const TimeSettingsParse = new TimeSettings();
TimeSettingsParse.parse(timeInMilliseconds);
Available arguments in the parse()
function:
Parameter | Type | Optional | Default | Description |
---|
timeInMilliseconds | number | ❌ | | Time in milliseconds to convert into a string. |
Examples:
const settings = new TimeSettings();
settings.parse("3 minutes and 8 seconds");
settings.parse("2 y, 4 months + 22 days + 9 hours");
Date
Format Date
Class DateSettings for the format:
Before call the format()
function, you have to create a class called DateSettings
. This function allows you to save the configuration you want in a variable, instead of rewrite everything.
new TimeSettings(options);
Available options in the configuration for the format()
function:
Parameter | Type | Optional | Default | Description |
---|
lang | string | ✅ | en | Language of date unities ('en' or 'fr'). |
format | string | ✅ | DD/MM/YYYY, HH:mm:ss.SSS | Format* of the string date. |
*Format: If you want to dodge some words in the format string, you can surround them with [
and ]
. It can be useful if you want to format a date, you can refer to the examples.
Function format():
Now the settings ready, you can call the function format()
which convert a date in milliseconds to a string with the format of your choice.
const DateSettingsFormat = new DateSettings({ lang: "en", format: "DD/MM/YYYY, HH:mm:ss.SSS" });
DateSettingsFormat.format(dateInMilliseconds);
Available arguments in the format()
function:
Parameter | Type | Optional | Default | Description |
---|
dateInMilliseconds | number | ❌ | | Date in milliseconds to convert into a string. |
Examples:
const settings_1 = new DateSettings({ lang: "en", format: "[The] DDD [of] MMMM YYYY [at] hh:mm AA" });
const settings_2 = new DateSettings({ lang: "en", format: "[It's] HH:mm:ss.SSS" });
settings_1.format(1660594792908);
settings_2.format(1660594792908);