Regular expression for matching HH:MM time format

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Regular expression for matching HH:MM time format

🕰️ A Guide to Regular Expressions for Matching HH:MM Time Format 📝

Are you struggling to find the perfect regular expression to match the HH:MM time format? Look no further! In this post, we'll address a common issue where the leftmost digit in the hour is optional. We'll provide you with easy solutions that work in both JavaScript and PHP. So let's dive in and solve this problem! 💪

The Initial RegEx

First, let's take a look at the initial regular expression provided:

^[0-2][0-3]:[0-5][0-9]$

This expression matches time in the HH:MM format, ranging from 00:00 to 23:59. It works perfectly for this specific scenario. However, if you want to match both HH:MM and H:MM, we need to make some adjustments.

Making the Leftmost Digit Optional

To make the leftmost digit optional, we need to modify the regular expression. Here's how we can do it:

^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$

Let's break it down:

  • ([0-1]?[0-9]|2[0-3]) matches the hour part of the time.

    • [0-1]?[0-9] allows for hours from 00 to 19. The [0-1]? part makes the leftmost digit (0 or 1) optional.

    • 2[0-3] matches the hours from 20 to 23 exactly.

  • :[0-5][0-9] matches the minutes part of the time, ranging from 00 to 59.

This modified expression now matches both HH:MM and H:MM formats.

Implementation in JavaScript and PHP

Now that we have our regular expression, let's see how we can implement it in both JavaScript and PHP.

JavaScript

To use the regular expression in JavaScript, we can utilize the test() method provided by the RegExp object. Here's an example:

const regex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/;

const time = '9:30';
const isTimeValid = regex.test(time);

console.log(isTimeValid); // Output: true

PHP

In PHP, we can make use of the preg_match() function to test our regular expression. Here's an example:

$regex = '/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/';

$time = '18:45';
$isTimeValid = preg_match($regex, $time);

var_dump($isTimeValid); // Output: int(1)

📣 Time to Put it into Action!

Now that you have the power of this regular expression, it's time to put it to use! Update your code, test it out, and enjoy the flexibility of matching both HH:MM and H:MM time formats.

If you found this guide helpful or have any additional questions, feel free to share your thoughts in the comments section below. Let's keep the conversation going! 🗣️💬

Happy coding! 💻✨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my