I have done a shell script to validate the coherence of a date (not the fromat) like that:
DATE="20131231"
date -d "${DATE}" "+%Y/%m/%d" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "INVALID DATE!"
else
echo "VALID DATE"
fi
The wanted result is OK under GNU/Linux and Unix/OpenSolaris 10.
DATE="20131231" ==> VALID DATE
DATE="20131232" ==> INVALID DATE!
DATE="20131331" ==> INVALID DATE!
But is not OK under IBM AIX OS. It always display "INVALID DATE!".
DATE="20131231" ==> INVALID DATE!
DATE="20131232" ==> INVALID DATE!
DATE="20131331" ==> INVALID DATE!
Thanks for your response.
If you have a recent enough Perl distribution (5.10 I think), this is likely very portable:
$ validate_date() {
perl -MTime::Piece -e '
eval {Time::Piece->strptime(q{'"$1"'}, "%Y%m%d")};
exit 1 if $@
'
}
$ date=20131231; validate_date $date && echo VALID || echo INVALID
VALID
$ date=20131232; validate_date $date && echo VALID || echo INVALID
INVALID
$ date=20131233; validate_date $date && echo VALID || echo INVALID
INVALID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.