Bash : Check if a string is a good date relative to the given format

date --date="now" +%Y_%m_%d_%Hh%M
Return 2025_10_22_17h03 as expected.

Now I have a string variable which contains "2025_10_22_17h03"
And I want to check if it is a good date relative to the format %Y_%m_%d_%Hh%M

The test return false for any tested date

The test is : [[ "$(date -d "2025_10_22_17h03" "+%Y_%m_%d_%Hh%M" 2>/dev/null)" == "2025_10_22_17h03" ]]

Any help is welcome.

Have you tried to look at the output of your date command?

Yes
date -d "2025_10_22_17h03" "+%Y_%m_%d_%Hh%M"
Return :
date: invalid date ‘2025_10_22_17h03’
Return code is : 1

Try adding the --debug flag. It seems to be interpreting the “h03” as a time zone. The format is very flexible, but obviously has its limits.

I messed around with the format string and found this works “2025-10-22 17:03”

The format string is only used for outputs and not for inputs.