Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

time-date-tools

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

time-date-tools

`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*

  • 0.0.26
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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
  • Importation
  • Time
    • Format : Convert a time in milliseconds to a time in string.
    • Parse : Convert a time in string to a time in milliseconds.
  • Date
    • Format : Convert a date in milliseconds to a date in string.

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 ].


Available token for the time format:
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


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(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

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 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.


Available token for the date format:
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

Keywords

FAQs

Package last updated on 22 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc