To verify that a date string is in a specific format, you can coerce the date into the format you want and verify that the string is the same as you started with:
$ format='%Y/%m/%d %H:%M:%S'
$ invalid_date='2009/05/11 07:30'
$ [ "$(date --date="$invalid_date" +"$format")" = "$invalid_date" ]
$ echo "$?"
1
$ valid_date='2009/05/11 07:30:00'
$ [ "$(date --date="$valid_date" +"$format")" = "$valid_date" ]
$ echo "$?"
0
This will only work for date strings which are date
compatible (you can't specify the input formatting directly in date
).