About
time-date-tools is a powerful Node.js module that allows you to easily manage time and date.
Features
Installation
npm install --save time-date-tools
Examples
const tdt = require('time-date-tools');
Convert milliseconds to string time
The function formatTime()
converts time in milliseconds to a time in string with the format of your choice.
tdt.formatTime(time, options);
Here is the list of all the arguments available:
Parameter | Type | Optional | Default | Description |
---|
time | number | ❌ | | Time in milliseconds to convert into string. |
format | string | ✅ | undefined | Format of string time returned. If not given, returns all the information about the time. |
lang | string | ✅ | en | Language of time unities ('en' or 'fr'). |
You can enclose certain parts of your format with brackets []
, the values contained in these brackets will be displayed even if they are null.
Some usage examples:
tdt.formatTime(654686145655, {});
tdt.formatTime(65364,
{ format: 'M:S.MS' }
);
tdt.formatTime(
449155098,
{
format: 'D DD, h:M:S.ms',
lang: 'en'
}
);
tdt.formatTime(31556927894,
{
format: '[y YY, mo MMOO,] d DD H:M:S.MS',
}
);
tdt.formatTime(31556927894,
{
format: 'y YY, mo MMOO, d DD H:M:S.MS',
}
);
Convert milliseconds to string date
The function formatDate()
converts date in milliseconds to a date in string with the format of your choice.
tdt.formatDate(date, options);
Here is the list of all the arguments available:
Parameter | Type | Optional | Default | Description |
---|
time | number | ❌ | | Date in milliseconds to convert into string. |
format | string | ✅ | undefined | Format of string date returned. If not given, returns all the information about the date. |
lang | string | ✅ | en | Language of date unities ('en' or 'fr'). |
Some usage examples:
tdt.formatDate(Date.now(), {});
tdt.formatDate(1654349360501,
{ format: 'M:S.MS' }
);
tdt.formatTime(Date.now(),
{
format: 'DD D/MO/Y at H:M:S.MS',
lang: 'en'
}
);
tdt.formatDate(0,
{
format: 'DD d-/MO/Y at H:M:S.MS',
}
);
tdt.formatTime(1623324464826,
{
format: 'DD d MMOO Y',
}
);
Convert string time to milliseconds time
The function parseTime()
converts time in string to a time in milliseconds.
tdt.parseTime(time, options);
Here is the list of all the arguments available:
Parameter | Type | Optional | Default | Description |
---|
time | number | ❌ | | Time in string to convert into milliseconds. |
msOff | boolean | ✅ | false | Returns the time in seconds instead of milliseconds. |
Some usage examples:
tdt.parseTime('2 days', {});
tdt.parseTime('5m 3s',
{ msOff: true }
);
Datas
Availables tokens for the date format.
Token | Description | Output |
---|
Y | four-digit year | 2022 |
y | two-digit year | 22 |
MMOO | month name | January |
MO | two-digit month | 01 |
mo | month | 1 |
DD | day name | Monday |
D | two-digit day | 01 |
D- | two-digit day with indice | 01st |
d | day | 1 |
d- | one-digit day with indice | 1st |
H | two-digit hour | 09, 14 |
h | hour | 9, 14 |
M | two-digit minute | 05, 34 |
m | minute | 5, 34 |
S | two-digit second | 02, 58 |
s | second | 2, 58 |
MS | three-digit millisecond | 009, 158 |
ms | millisecond | 9, 158 |
Availables tokens for the time format.
Token | Description | Output |
---|
YY | year unity (long) | year, years |
yy | year unity (short) | y |
Y | four-digit year | 2022 |
y | two-digit year | 22 |
MMOO | month unity (long) | month, months |
mmoo | month unity (short) | m |
MO | two-digit month | 01 |
mo | month | 1 |
DD | day unity (long) | day, days |
dd | day unity (short) | d |
D | two-digit day | 01 |
d | day | 1 |
HH | hour unity (long) | hour, hours |
hh | hour unity (short) | h |
H | two-digit hour | 09, 14 |
h | hour | 9, 14 |
MM | minute unity (long) | minute, minutes |
mm | minute unity (short) | m |
M | two-digit minute | 05, 34 |
m | minute | 5, 34 |
SS | second unity (long) | seconds, second |
ss | second unity (short) | s |
S | two-digit second | 02, 58 |
s | second | 2, 58 |
MMSS | millisecond unity (long) | millisecond, milliseconds |
mmss | millisecond unity (short) | ms |
MS | three-digit millisecond | 009, 158 |
ms | millisecond | 9, 158 |