1.0.2 • Published 5 years ago

convertible-js v1.0.2

Weekly downloads
33
License
MIT
Repository
github
Last release
5 years ago

convertible-js :clock1:

dependencies Status npm version License: MIT

Convert 12-hour time string to 24-hour time and vice versa with flexible formatting.

The library is available as an npm package. To install the package run:

npm install convertible-js --save
# or with yarn
yarn add convertible-js

Docs

convertTime(input, options)

Converts 12-hour time string to 24-hour time and vice versa.

Arguments

NameTypeDescription
inputstringtime to convert or format
options (optional)Objectoptions object for converting and formatting
Options (Optional)

convertTo: Optional. The time type to convert to. Can only be either '12-hour' or '24-hour'

format: Optional. The desired time format.

UnitPatternResult examples
Hour 1-12h1, 2, ..., 11, 12
hh01, 02, ..., 11, 12
Hour 1-23H0, 1, 2, ..., 23
HH00, 01, 02, ..., 23
Minutem0, 1, ..., 59
mm00, 01, ..., 59
AM, PMaam, pm
AAM, PM

Returns ( string | undefined )

string - By default, it returns the opposite format of the given time (12-hour to 24-hour) unless convertTo is specified in the second argument object.

undefined - If arguments provided are invalid or if an error occured while rounding the time. The library will not throw any error.

Usage

// CommonJS
// var convertTime = require('convertible-js').default;

// ES2015
import convertTime from 'convertible-js';

convertTime('12:04am'); // => '00:04'
convertTime('10:00am'); // => '10:00'
convertTime('2pm'); // => '14:00'
convertTime('1am'); // => '01:00'

convertTime('10:00:50am'); // => '10:00:50'
convertTime('10:00:54pm'); // => '22:00:54'
convertTime('10:00pm'); // => '22:00'
convertTime('12:06:34PM'); // => '12:06:34'
convertTime('12:06:34AM'); // => '00:06:34'


convertTime('00:00'); // => '12:00 AM'
convertTime('10:00'); // => '10:00 AM'
convertTime('22:00:54'); // => '10:00:54 PM'
convertTime('23:59:54'); // => '11:59:54 PM'

// Using `convertTo`
convertTime('00:00', { convertTo: '24-hour' }); // => '00:00', since it's already in 24-hour
convertTime('00:01', { convertTo: '12-hour' }); // => '12:01 AM'

// Using `format`
convertTime('10:34pm', { convertTo: '24-hour', format: 'HH:mm' }); // => '22:34'
convertTime('10:34pm', { convertTo: '12-hour', format: 'HH:mm A' }); // => '10:34 PM'

License

MIT © Ana Liza Pandac