Skip to content

Instantly share code, notes, and snippets.

@MarketingPip
Last active August 10, 2023 04:05
Show Gist options
  • Save MarketingPip/de94001b206852f1f7534ee9f89c49e2 to your computer and use it in GitHub Desktop.
Save MarketingPip/de94001b206852f1f7534ee9f89c49e2 to your computer and use it in GitHub Desktop.
A regular expression for matching human readable time. Such as 5pm, 5.30 pm, 12 noon.

Regex for Human Time

This regular expression is for finding common time format in string. Example 5:18pm

(1[0-2]|0?[1-9]|(1[0-2]|0?[1-9]):([0-5][0-9])|(1[0-2]|0?[1-9]).([0-5][0-9]))\s?([AP]M|[AP].M|o'clock|oclock|noon|afternoon|in the morning|in the evening|this morning|this evening|later this evening|tonight|at night|at noon|midnight|[ap]m|[ap].m)

Tests

at 5:12pm oclock tommo
at 5 o'clock tommo
5pm tommorow
at 5 am tommorow
at 5PM tommorow

Results from tests

Match 1
1.	5:12
2.	5
3.	12
4.	pm
Match 2
1.	5
2.	None
3.	None
4.	o'clock
Match 3
1.	5
2.	None
3.	None
4.	pm
Match 4
1.	5
2.	None
3.	None
4.	am
Match 5
1.	5
2.	None
3.	None
4.	PM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment