Validate date format in a shell script
Use date
date "+%d/%m/%Y" -d "09/99/2013" > /dev/null 2>&1
is_valid=$?
The date string must be in "MM/DD/YYYY" format.
If you do not get 0 then date is in invalid format.
check if argument is a valid date in bash shell
You can check with date -d "datestring"
So date -d "12/31/2012"
is valid, but using hyphens, e.g. date -d "12-31-2012"
, is not valid for date
.
You can also use words: date -d 'yesterday'
or date -d '1 week ago'
are both valid.
Bash validate date
The BSD date
that ships with Mac OS X doesn't support the -d
option (or rather, it uses -d
for something entirely different). Either install GNU date
, or use the following to validate your input string:
date -f "%Y-%m-%d" -j "2012-02-29" >/dev/null 2>&1
The -f
provides the input format, and the -j
tells date
to simply output the date, not attempt to set the system clock.
Validate a date in shell script (with the BSD date)
FreeBSD
date
does not support the -d
flag,
date -f "%Y-%m-%d" -j "2017-01-28" >/dev/null 2>&1
is_valid=$?
(or)
date -f "%Y-%m-%d" -j "2017-01-28" >/dev/null 2>&1 && printf "Date validation success\n" || printf "Date validation fail\n"
DATE validation using regex in awk
In GNU awk
the \d
and \z
sequences are not valid regex operators (a quick web search doesn't show these as valid regex operators in a couple others flavors of awk
though by no means an exhaustive search).
I'd suggest replacing the \d
with [0-9]
or [[:digit:]]
; as for the \z
you could try \>
or \y
.
One other issue is the use of .
as a wildcard match in the time component; if you know all times will use a colon (:
) as a delimiter then I'd use an explicit colon.
Rolling these changes into the current code (and fixing a couple cut-n-paste/syntax issues):
awk -F '|' 'BEGIN {OFS=FS}
{ if ($1 ~ /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2} [AP]M\y/)
print
}'
This generates:
04/21/2014 02:04:55 AM|34536
12/31/2021 03:29:15 AM|87612
NOTES:
- obviously (?) this code assumes a specific date/time format and thus ...
- this code will not match on other valid date/time formats (eg, won't match on
2021/12/31
) - the use of
[0-9]
opens you up to matching on strings that are not valid dates and/or times, eg, this code will match on99/99/2022
and99:99:99
); OP can address some of these by limiting the series of digits that can be matched in a given position (eg,[0-2][0-9]
for hours) but even this is problematic since29
will match but is not a valid hour - as alluded to in comments ... validating dates/times is doable but will require a good bit more code (alternatively run a web search on
bash awk validate dates times
for additional ideas)
Related Topics
While Do Loop and Variables in a Bash Script
Ftdi D2Xx Conflict with Ftdi_Sio on Linux - How to Remove Ftdi_Sio Automatically
Segfault with Rip-Relative Addressing on Linux
-Bash: /Usr/Bin/Yum: /Usr/Bin/Python: Bad Interpreter: No Such File or Directory
Installed Go Binary Not Found in Path on Alpine Linux Docker
Must My Pidfile Be Located in /Var/Run
Fuse Error: Transport Endpoint Is Not Connected
Using Bash Script to Find Line Number of String in File
Join on First Column of Two Files
Tiny "Manually" Created Elf Giving Segmentation Fault
Bash Echo with an $ Character Outside the String
Amazon Linux: "Apt-Get: Command Not Found"
What Is Preemption/What Is a Preemtible Kernel? What Is It Good For