simple-time-input-engine
Engine for implementing a time input field that accepts flexible inputs. It has support for 12 and 24 hours formats.
Installation
npm install simple-time-input-engine --save
API
parseInputChange
Receives an input (newValue
), a previous input (previousTime
) and a clockMode
, and outputs a time string in 24-hour format.
const valid time = console // trueconsole // "06:00"
- The input (
newValue
) is flexible, you can pass different strings such as06:00
,6:30
,630
,630a
,630p
,6a
,6p
,6
, etc. If you pass an empty string, the result will be an empty string. - The previous input (
previousTime
) must be in 24-hour format, or an empty string. - If a non-supported time input is passed, it'll return an object
{ "valid": false }
with notime
key. clockMode
is used for activating inference of the period of day based on the previous value. So if you pass630
and the previous time is13:00
(1pm), the new value will be18:30
(6:30pm). If the previous time is01:00
(1am), it will infer the new time as06:30
(6:30am).- This library assumes your implementation of the input field stores the time internally in 24-hour format, so
parseInputChange
will always return in 24-hour despite theclockMode
passed.clockMode
is only used for activating the period inference described above.
formatTimeForDisplay
Transforms the input time in 24-hour format to an output in 12 or 24-hour format, based on the clockMode
passed.
console // "" console // "5:00am"console // "5:00pm" console // "05:00"console // "17:00"