bash - Test if variable is a date with the correct format -
bash - Test if variable is a date with the correct format -
i test if variable date (not problem), if variable have right date format (yyyy-mm-dd).
i tried :
export date_refresh=01/01/1900 if ! date -d $date_refresh "+%y-%m-%d"; echo "$date_refresh not valid date. expected format : yyyy-mm-dd" fi
but doesn't work.
i can seek :
if [[ $date_refresh == [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] ]]
but don't want have date 33/19/2000...
you can utilize snippet:
isvaliddate() { if [[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && date -d "$1">/dev/null 2>&1; echo "valid" else echo "invalid" fi; }
testing:
isvaliddate "1900-12-25" valid isvaliddate "1900-14-25" invalid isvaliddate "01/01/1900" invalid
bash date
Comments
Post a Comment