Shell Script to find the validity of a given date

in Categories Time and Date last updated April 5, 2008

Date is entered in mm/dd/yyyy format using a keyboard. Various conditions are applied to make sure date is valid. Read the program comments to get idea about program logic.


Share this on:

2 comment

  1. try this code:

    DATE=”$*”

    [[ “${DATE}” != @(((([123][0]|[012][1-9])|3[1])?([ /.-])(0[13578]|1[02])?([ /.-])(19|20)[0-9][0-9])|(([123][0]|[012][1-9])?([ /.-]) (0[469]|11)?([ /.-])(19|20)[0-9][0-9])|(([12][0]|[01][1-9]|2[1-8])?([ /.-])02?([ /.-])(19|20)[0-9][0-9])|(29?([ /.-])02?([ /.-]) (19|20(0[48]|[2468][048]|[13579][26])))) ]] && echo error || echo good

  2. It’s both simpler and more reliable to use GNU date:

    #/bin/bash
    if [ “$( date -d “$1″ +%F 2>&1 | grep invalid )” = “” ] ; then echo “$1 is a valid date”; fi

    Have a question? Post it on our forum!