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.

  • 0.0.14
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15
decreased by-91.43%
Maintainers
1
Weekly downloads
 
Created
Source

About

time-date-tools is a powerful Node.js module that allows you to easily manage time and date.

Features

  • Installation
  • Convert milliseconds to string time
  • Convert milliseconds to string date
  • Convert string time to milliseconds time

Installation

npm install --save time-date-tools

Examples

const tdt = require('time-date-tools'); // Import the module

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:

ParameterTypeOptionalDefaultDescription
timenumberTime in milliseconds to convert into string.
formatstringundefinedFormat of string time returned. If not given, returns all the information about the time.
langstringenLanguage 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, {});
/*
{
    y: 20, mo: 8, d: 29, h: 1, m: 0, s: 25, ms: 655,
    YY: 'years', MMOO: 'months', DD: 'days', HH: 'hour', MM: 'minute', SS: seconds', MMSS: 'milliseconds',
    Y: '20', MO: '08', D: '29', H: '01', M: '00', S: '25', MS: '655',
    yy: 'y', mmoo: 'mo', dd: 'd', hh: 'h', mm: 'm', ss: 's', mmss: 'ms'
}
*/

tdt.formatTime(65364,
    { format: 'M:S.MS' }
); // 01:05.364

tdt.formatTime(
    449155098,
    {
        format: 'D DD, h:M:S.ms',
	lang: 'en'
    }
); // 05 days, 4:45:55.98

tdt.formatTime(31556927894,
    {
        format: '[y YY, mo MMOO,] d DD H:M:S.MS',
    }
); // 1 year, 0 month, 01.894

tdt.formatTime(31556927894,
    {
        format: 'y YY, mo MMOO, d DD H:M:S.MS',
    }
); // 1 year, 01.894


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:

ParameterTypeOptionalDefaultDescription
timenumberDate in milliseconds to convert into string.
formatstringundefinedFormat of string date returned. If not given, returns all the information about the date.
langstringenLanguage of date unities ('en' or 'fr').

Some usage examples:


tdt.formatDate(Date.now(), {});
/*
{
    date: 2022-06-04T13:28:31.790Z,
    milliseconds: 790,
    millisecondsFull: '790',
    seconds: 31,
    secondsFull: '31',
    minutes: 28,
    minutesFull: '28',
    hours: 15,
    hoursFull: '15',
    days: 'sunday',
    daysNumber: 4,
    daysNumberIndice: '4',
    daysFullNumber: '04',
    daysFullNumberIndice: '04',
    months: 'june',
    monthsNumber: 6,
    monthsFullNumber: '06',
    years: '22',
    yearsFull: 2022
}
*/

tdt.formatDate(1654349360501,
    { format: 'M:S.MS' }
); // 29:44.883

tdt.formatTime(Date.now(),
    {
        format: 'DD D/MO/Y at H:M:S.MS',
        lang: 'en'
    }
); // sunday 04/06/2022 at 15:31:00.760

tdt.formatDate(0,
    {
        format: 'DD d-/MO/Y at H:M:S.MS',
    }
); // friday 1nd/01/1970 at 01:00:00.000

tdt.formatTime(1623324464826,
    {
        format: 'DD d MMOO Y',
    }
); // friday 10 june 2021


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:

ParameterTypeOptionalDefaultDescription
timenumberTime in string to convert into milliseconds.
msOffbooleanfalseReturns the time in seconds instead of milliseconds.

Some usage examples:


tdt.parseTime('2 days', {}); // 172800000

tdt.parseTime('5m 3s',
    { msOff: true }
); // 303


Datas

Availables tokens for the date format.

TokenDescriptionOutput
Yfour-digit year2022
ytwo-digit year22
MMOOmonth nameJanuary
MOtwo-digit month01
momonth1
DDday nameMonday
Dtwo-digit day01
D-two-digit day with indice01st
dday1
d-one-digit day with indice1st
Htwo-digit hour09, 14
hhour9, 14
Mtwo-digit minute05, 34
mminute5, 34
Stwo-digit second02, 58
ssecond2, 58
MSthree-digit millisecond009, 158
msmillisecond9, 158

Availables tokens for the time format.

TokenDescriptionOutput
YYyear unity (long)year, years
yyyear unity (short)y
Yfour-digit year2022
ytwo-digit year22
MMOOmonth unity (long)month, months
mmoomonth unity (short)m
MOtwo-digit month01
momonth1
DDday unity (long)day, days
ddday unity (short)d
Dtwo-digit day01
dday1
HHhour unity (long)hour, hours
hhhour unity (short)h
Htwo-digit hour09, 14
hhour9, 14
MMminute unity (long)minute, minutes
mmminute unity (short)m
Mtwo-digit minute05, 34
mminute5, 34
SSsecond unity (long)seconds, second
sssecond unity (short)s
Stwo-digit second02, 58
ssecond2, 58
MMSSmillisecond unity (long)millisecond, milliseconds
mmssmillisecond unity (short)ms
MSthree-digit millisecond009, 158
msmillisecond9, 158

Keywords

FAQs

Package last updated on 07 Jun 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