Search code examples
bashshellunixbsd

Validate a date in shell script (with the BSD date)


I usually validate the date in my shell scripts with that command :

date "+%Y-%m-%d" -d "2017-01-28" > /dev/null  2>&1
is_valid=$?

This works perfectly with the GNU date.. But not with the BSD one. Any idea how to do the same with the BSD version of date ?


Solution

  • 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"