# 日付の妥当性チェック(ValidateDate,不正な日付,IllegalDate)

# Bash

date "+%Y%m%d" -d "$1" > /dev/null 2&>1  # status code -1
function IsYYYYMMDD()
{
    if [ `expr "$1"` -ne 8 ]; then
        return 1
    fi

    date "+%Y%m%d" -d "$1" > /dev/null 2&>1  # stdout, stderr は使わず、status code を使う
}

if IsYYYYMMDD "$DT"; then  # `[ .. ]` で囲わない
    echo "$DT is date"
else
    echo "$DT is not date"
fi

# PHP

$d = new DateTime('2015-03-32'); // => Error
$d = new DateTime('2015/02/31'); // => 2015/03/03

# Python

d = datetime.datetime.strptime('2015-03-32', '%Y-%m-%d').date() # ValueError

# Ruby

d = Date::parse('2015-03-32')  # 例外が発生する ArgumentError

[ 📩 ご意見 ]