2.0.0 • Published 3 years ago

jmot-lib v2.0.0

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

jmot-lib (JavaScript Manipulation of Time Library) :milky_way:

A JavaScript utility library that converts decimal to time.

Sometimes we end up having to work with non-standard time values. For example 1.5 hrs means one and a half hours.

This library aims to make it easy to do manipulations on time. So that, 1.5 hrs becomes 1:30 and where 1.5 + 14:00 returns 15:30.

HoursTimeHoursTime
00:001212:00
0.0833330:0512.08333312:05
0.1666670:1012.16666712:10
0.250:1512.2512:15
0.3333330:2012.33333312:20
0.4166670:2512.41666712:25
0.50:3012.512:30
0.5833330:3512.58333312:35
0.6666670:4012.66666712:40
0.750:4512.7512:45
0.8333330:5012.83333312:50
0.9166670:5512.91666712:55
11:001313:00

:information_source: 1 hour is equal to 60 minutes.

Usage

Installation

using npm:

npm install jmot-lib

Code Example

ESM support:

import { dectotime, adddectotime } from 'jmot-lib';

var x = dectotime('4.5');
console.log('result: ', x);
// result: 4:30

var y = adddectotime('1.75','14:00');
console.log('result: ', y);
// result: 15:45

CJS(CommonJS) and UMD(Universal Module Definition) also supported.

String -> String

dectotime takes a string as input and outputs a string.

Gotchas :facepunch:

  • :clock12: 24 Hour Format

    The api supports up to 24-hour decimal time. However, it will allow all values.

  • :ghost: Boo O'Clock

    dectotime takes a string as input.

    dectotime('boo');
    // boo:00

Ensure you validate the value being passed in beforehand.

Functions

adddectotime takes two string inputs ('1.0','12:00') a decimal and a time. The two values are summed together and returned as a string.

currenttime returns current time based on your locale setting. The default currenttime() returns your local short(HH:MM)time in UTC format.

Optional parameters: currenttime(_locale_,_timeformat_). locale(specify a language format and or location) and timeformat Use 'HH:MM:SS' to return a long time. See examples below:

import { currenttime } from 'jmot-lib';

currenttime();
// "13:15"

currenttime('en');
// "1:15 PM"

currenttime('en-GB');
// "13:15"

currenttime('in-ID');
// "১:১৫ PM"

currenttime('en-US', 'HH:MM:SS');
// "1:15:03 PM"

List of supported locales and example outputs.

dectotime takes a decimal string as input and returns a string time HH:MM.

Contribute :purple_heart:

Become a contributor to jmot!

If you've found a bug, please create a new issue or a pull request.

All feedback welcome! :raising_hand: