Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 1.02 KB

README.md

File metadata and controls

42 lines (30 loc) · 1.02 KB

ParseTime

Summary

A short JavaScript function that parses a time string and returns a Date object. It handles a wide variety of time input formats.

Details

Takes a string representing a time and returns a Date object with January 1, 1970 and the specified time. Returns null if given a null or empty time string. Returns a Date object representing an invalid date if the time string is invalid.

Example formats:

1:32 PM
1:32 p.m.
1:32p
1p
13:32
1332

See test/tests.html for the full list of supported time input formats.

Usage

Call the function, passing in the string to parse.

parseTime('9:45 AM'); // -> Date object (Jan 01 1970 09:45)
parseTime(''); // -> null
parseTime('abc'); // -> Date object (Invalid Date)

You can use the utility functions to detect invalid dates and format the time.

isValidDate(parseTime('9:45 AM')); // -> true
isInvalidDate(parseTime('abc')); // -> true
dateToTimeString(parseTime('9am')); // => '9:00 AM'

License

Public Domain