convertible-js 🕐
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
Name | Type | Description |
---|---|---|
input | string | time to convert or format |
options (optional) | Object | options 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.
Unit | Pattern | Result examples |
---|---|---|
Hour [1-12] | h | 1, 2, ..., 11, 12 |
hh | 01, 02, ..., 11, 12 | |
Hour [1-23] | H | 0, 1, 2, ..., 23 |
HH | 00, 01, 02, ..., 23 | |
Minute | m | 0, 1, ..., 59 |
mm | 00, 01, ..., 59 | |
AM, PM | a | am, pm |
A | AM, 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; ; // => '00:04'; // => '10:00'; // => '14:00'; // => '01:00' ; // => '10:00:50'; // => '22:00:54'; // => '22:00'; // => '12:06:34'; // => '00:06:34' ; // => '12:00 AM'; // => '10:00 AM'; // => '10:00:54 PM'; // => '11:59:54 PM' // Using `convertTo`; // => '00:00', since it's already in 24-hour; // => '12:01 AM' // Using `format`; // => '22:34'; // => '10:34 PM'
License
MIT © Ana Liza Pandac