0.0.26 • Published 2 years ago

time-date-tools v0.0.26

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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. If you have any question, you can join our community on Discord.

Features

Installation

$ npm install --save time-date-tools

Usage

Importation

import tdt from "time-date-tools";          // Es Modules
const tdt = require("time-date-tools");     // CommondJS

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:

ParameterTypeOptionalDefaultDescription
langstringenLanguage of time unities ('en' or 'fr').
formatstringY YYYY, M MMMM, W WWWW, D dddd, h HH, m MM, s SS, sss SSSSFormat* of the string time.
precisionbooleantrueIf false, it will round and return the bigger nonnull unity. Else, all the unities are returned.
longbooleantrueComplete 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 ].

TokenMeaningExamples of output
YYYYYear unityyears, year
yyyyYear unity (short)y
YYear value1
MMMMMonth unitymonths, month
mmmmMonth unity (short)mo
MMonth value2
WWWWWeek unityweeks, week
wwwwWeek unity (short)w
WWeek value3
DDDDDay unitydays, day
ddddDay unity (short)d
DDay value4
HHHour unityhours, hour
hhHour unity (short)h
hHour value5
MMMinute unityminutes, minute
mmMinute unity (short)m
mMinute value6
SSSecond unityseconds, second
ssSecond unity (short)s
sssSecond value7
SSSSMillisecond unitymilliseconds, millisecond
ssssMillisecond unity (short)ms
sssMillisecond value8

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:

ParameterTypeOptionalDefaultDescription
timeInMillisecondsnumberTime 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); // 6 minutes, 1 second, 410 milliseconds settings_2.format(361410); // 0 year, 0 month, 6 minutes, 1 second, 410 milliseconds

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); // 6 days settings_4.format(486000000); // 6d settings_5.format(486000000); // 0 year, 0 month, 0 week, 5 days, 15 hours, 0 minute, 0 second, 0 millisecond

<br>

### Parse Time

<br>

> #### 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.
```ts
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(timeInString);

Available arguments in the parse() function:

ParameterTypeOptionalDefaultDescription
timeInStringnumberTime in string to convert into milliseconds.

Examples:

const settings = new TimeSettings();

settings.parse("3 minutes and 8 seconds"); // 188000 settings.parse("2 y, 4 months + 22 days + 9 hours"); // 75566252000

<br>

## Date

### Format Date

<br>

> #### 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.
```ts
new DateSettings(options);

Available options in the configuration for the format() function:

ParameterTypeOptionalDefaultDescription
langstringenLanguage of date unities ('en' or 'fr').
formatstringDD/MM/YYYY, HH:mm:ss.SSSFormat* 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.

TokenMeaningExamples of output
YYYYFour-digit year1970, 2022
YYTwo-digit year70, 22
MMMMMonth name (long)January, December
MMMMonth name (short)Jan, Dec
MMTwo-digit month01, 12
MOne-digit month1, 12
ddddDay of week (long)monday, sunday
dddDay of week (short - three-digit)mon, sun
ddDay of week (short - two-digit)mo, su
DDDOrdinal notation of date1st, 2nd, 3rd, 4th
DDTwo-digit date01, 31
DOne-digit date1, 31
HHTwo-digit 24-hour01, 23
HOne-digit 24-hour1, 23
hhTwo-digit 12-hour01, 11
hOne-digit 12 hour1, 11
mmTwo-digit minute01, 59
mOne-digit minute1, 59
ssTwo-digit second01, 59
sOne-digit second1, 59
SSSThree-digit millisecond001, 999
SSTwo-digit millisecond01, 99
SOne-digit millisecond1, 9
RRoman yearMMXXII
AAMeridiem (uppercase with ellipsis)A.M., P.M.
AMeridiem (uppercase)AM, PM
aaMeridiem (lowercase with ellipsis)a.m., p.m.
aMeridiemam, pm

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:

ParameterTypeOptionalDefaultDescription
dateInMillisecondsnumberDate 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" }); // sunday 14/08/2022, 09:35:33.766
const settings_2 = new DateSettings({ lang: "en", format: "[It's] HH:mm:ss.SSS" });

settings_1.format(1660594792908); // The 15th of august 2022 at 10:19 P.M. settings_2.format(1660594792908); // It's 22:19:52.908

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.2

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.1

2 years ago