@flat/time
Parses time input with no relation to dates,
with the option to convert to the next immediate corresponding Date.
Built to solve this problem. Forked from zackdever/time.
$ npm install @flat/time
var t = Time('2p');
t.hours();
t.militaryHours();
t.minutes();
t.period();
t.toString();
t.nextDate();
t.format('hh:mm AM')
t.isValid();
Time.isValid('99:12');
Examples
Some example uses can be viewed in examples.html.
Parses strings such as "8:20" into a Date-less Time.
new Time('1')
new Time('13')
new Time('1:23')
If you fancy it, you can use safely drop the 'new'.
Time('1.23')
Time('123')
am/pm can optionally be specified.
Time('8:30 pm')
Time('3p')
Time('3 A.M.')
Converts Time into the next corresponding JavaScript Date.
Time('415').nextDate()
Time('2').nextDate()
Time('2 pm').nextDate()
Does validation statically...
Time.isValid('8:00')
Time.isValid('12:60')
Time.isValid('13:23')
... or after construction.
Time('1').isValid()
Time('12.0').isValid()
Time('12:202').isValid()
There's basic formatting
Time('2:30p').format('hh:mm A');
Time('2:30p').format('HH:mm');
Time('12 am').format('h: p');
Time('220 a').format('h: p');
Time('7').format('h: p');
Accepts numbers too.
Time(1).isValid()
Military (24-hour) time support
Time('13').format('hh:mm AM');
Time('2:30p').format('HH:mm');
Time('14:30').format('h:mm AM');
Time('0000').format('h:mm AM');
Time('2400').isValid();
ISO8601 time component (without seconds) support
Time('12:00 am').toISOString();
Time('12:00 pm').toISOString();
Time('11:00 pm').toISOString();
Test
$ npm test