How to Check Whether Date Argument Is in the yyyy-mm-dd Format

📅 2026/7/15 3:37:02
How to Check Whether Date Argument Is in the yyyy-mm-dd Format
1. OverviewWhen working with dates in Linux, it’s common to need to validate that a date argument is in the “yyyy-mm-dd” format. This format is widely used in Linux systems and is a standard format for many applications.In this tutorial, we’ll explore how to check whether a date argument is in the “yyyy-mm-dd” format in different ways.2. Checking the Variable’s Format Using RegexWhen we talk about if a variable’s value is in a certain format, we want to check if the value matches a specific pattern. This is precisely what Regex is good at.Therefore, let’s first look at how to solve the problem using Regex.Bash’sifstatement supports the~operator to apply Regex-based pattern match checking. Therefore, let’s create a simple shell script to check if user input is in the desired date format:$ cat check_date.sh #!/bin/bash INPUT_DT$1 if [[ $INPUT_DT ~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then echo The input $INPUT_DT is in the yyyy-mm-dd date format. else echo The input is NOT in the yyyy-mm-dd date format. fiCopyAs the cat‘s output above shows, ourcheck_date.shscript has anif-elseblock to check the input’s format. Then, it prints the corresponding information depending on the check result.The Regex “^[0-9]{4}-[0-9]{2}-[0-9]{2}$” is pretty straightforward. But it’s worth mentioning thatwe shouldn’t wrap the Regex in quotes, or the~check fails.Next, let’s test if our script works for different inputs:$ ./check_date.sh 1992-08-08 The input 1992-08-08 is in the yyyy-mm-dd date format. $ ./check_date.sh 08.08.1992 The input 08.08.1992 is NOT in the yyyy-mm-dd date format. $ ./check_date.sh 1992-AUG-08 The input 1992-AUG-08 is NOT in the yyyy-mm-dd date formatCopyAs we can see in the tests above, our script can correctly tell if our inputs are in the “yyyy-mm-dd” date format or not.Notably,although an argument passes the Regex-base check, it doesn’t mean it’s a valid date value.An example can explain it quickly:$ ./check_date.sh 1992-42-42 The input 1992-42-42 is in the yyyy-mm-dd date format.CopyAs we’ve seen, the value “1992-42-42” passes thecheck_date.shscript’s check. However, it’s not a valid date.3. Validating the Date VariableWe’ve learned that checking the date format using Regex doesn’t guarantee the value is a valid date. So now, let’s see how to validate a date value.3.1. Using thedateCommandWe canparse a static string value using thedatecommand with the-doption. Of course, we need to telldatewhich date format it should parse the string:$ cat validate_date.sh #!/bin/bash INPUT_DT$1 if date -d $INPUT_DT %Y-%m-%d /dev/null 21; then echo The input $INPUT_DT is a valid date string in the yyyy-mm-dd format. else echo The input $INPUT_DT is NOT a valid date string in the yyyy-mm-dd format. fiCopyTheifstatement checks the exit code of thedatecommand. If the input date string is a valid date value, the command returns0.Otherwise, the exit code is non-zero.We also added the “date … /dev/null 21” redirection to redirect bothstdoutandstderrto/dev/null,effectively discarding any output from thedatecommand.Next, let’s test the script with some valid and invalid date strings:$ ./validate_date.sh 1992-08-08 The input 1992-08-08 is a valid date string in the yyyy-mm-dd format. $ ./validate_date.sh 1992-AUG-08 The input 1992-AUG-08 is NOT a valid date string in the yyyy-mm-dd format. $ ./validate_date.sh 1992-42-42 The input 1992-42-42 is NOT a valid date string in the yyyy-mm-dd format. $ ./validate_date.sh 0000-00-00 The input 0000-00-00 is NOT a valid date string in the yyyy-mm-dd format.CopyAs the output shows, the script can check not only the argument’s format but also the validity of the date value.3.2. Usinggawkmktime()Functionawkis a powerful text processing tool. The widely used implementation GNU Awk provides a set of date- and time-related functions. For example,themktime()function allows us to create a date time value from string values. Thestrftime()can convert a date value to a string in the required format.Let’s first look at how we can perform the date validation usingawkand then understand how it works:$ cat validate_date_awk.sh #!/usr/bin/gawk -f BEGIN{ split(ARGV[1], array, /-/) ts mktime(array[1] array[2] array[3] 0 0 0) dtStr strftime(%Y-%m-%d,ts) if ( ARGV[1] dtStr ) print The input, ARGV[1], is a valid date string in the yyyy-mm-dd format. else print The input, ARGV[1], is NOT a valid date string in the yyyy-mm-dd format. }CopySo, as the script above shows, we perform the date validation in the following steps:Extract year, month, and day from the input as the “yyyy-mm-dd” format using thesplit()function and save the extracted elements in an array (array)Create aDateTimevalue (ts) using themktime()function and the extracted elements.As we’re interested in the timestamp part, we fill in zeros for hour, minute, and second fieldsConvert thetsdate variable to a string (dtStr) using thestrftime()function in the same formatCompare the initial input and the converted string. If they are equal, we can tell the input is validWe may have noticed that we didn’t check if the input value is in the “yyyy-mm-dd” format. Instead, we called thesplit()function directly. This is becauseawkhas a default value for undefined variables, such as 0 for numbers and an empty string for string variables. That’s to say, if the input isn’t in the required format, readingarray[1], [2],or[3]will still get a value. In this case, the converteddtStrwon’t be equal to the initial input. Thus, the validation won’t pass.So finally, let’s test the awk script with some inputs:$ ./validate_date_awk.sh 1992-08-08 The input 1992-08-08 is a valid date string in the yyyy-mm-dd format. $ ./validate_date_awk.sh 1992-aug-08 The input 1992-aug-08 is NOT a valid date string in the yyyy-mm-dd format. $ ./validate_date_awk.sh 0000-00-00 The input 0000-00-0 is NOT a valid date string in the yyyy-mm-dd format. $ ./validate_date_awk.sh 1992-42-42 The input 1992-42-42 is NOT a valid date string in the yyyy-mm-dd format.Copy4. ConclusionIn this article, we’ve learned how to check if a string is in the “yyyy-mm-dd” date format.Using Regex can straightforwardly check if the value matches the required format. However, it doesn’t guarantee that the value is a valid date.So, we’ve learned two approaches using thedatecommand andgawkto perform the date validation.