New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.

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

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

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(timeInMilliseconds);

Available arguments in the parse() function:

ParameterTypeOptionalDefaultDescription
timeInMillisecondsnumberTime in milliseconds to convert into a string.


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


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