[NTLUG:Discuss] verify that a text string is a valid date.

Chris Cox cjcox at acm.org
Thu Feb 2 13:12:29 CST 2006


Fred James wrote:
> Chris Cox wrote:
> 
>>
>> $ mydate="2006-02-01"
>> $ date -d "$mydate"
>> Feb 28 00:00:00 CST 2006
>> $ echo $?
>> 0
>> $ mybaddate="2006-02-29"
>> $ date -d "$mybaddate"
>> date: invalid date `2006-02-29'
>> $ echo $?
>> 1
>>  
>>
> Chris Cox
> My Mileage Did Vary - see below - as I was unable to duplicate the
> results on any of the 5 different types of machines/OSs I have available
> to me.  Did I do it wrong?

No... I don't think so... but you need to get the latest GNU date
if you haven't already.

What does date --version show?

I tested using GNU core utils 5.3.0
(that which comes with SUSE 9.3)




> Regards
> Fred James
> 
> Bash on Mandrake 9.2
> "
> $ mydate="2006-02-01"
> $ date -d "$mydate"
> Wed Feb  1 00:00:00 CST 2006
> $ echo $?
> 0
> $ mydate="2006-02-29"
> $ date -d "$mydate"
> Wed Mar  1 00:00:00 CST 2006
> $ echo $?
> 0
> "
> 
> Bourne on SGI IRIX 6.4, and 6.5.11m, Bourne on HP HP-UX B.11.11
> [ran this script]
> echo "mydate=\"2006-02-01\""
> mydate="2006-02-01"
> date -d "$mydate"
> echo $?
> echo "mydate=\"2006-02-29\""
> mydate="2006-02-29"
> date -d "$mydate"
> echo $?
> exit
> [got this back]
> "mydate="2006-02-01"
> Bad conversion
> Usage:
>        date [-u] [+format]
>        date [-u | -n] [[mmdd] HHMM | mmddHHMM[[cc]yy]][.ss]
>        date [-a [-]sss.fff]
> 1
> mydate="2006-02-29"
> Bad conversion
> Usage:
>        date [-u] [+format]
>        date [-u | -n] [[mmdd] HHMM | mmddHHMM[[cc]yy]][.ss]
>        date [-a [-]sss.fff]
> 1
> "
> 
> Bourne on Unisys Unix SVR4 2
> [ran this script]
> echo "mydate=\"2006-02-01\""
> mydate="2006-02-01"
> date -d "$mydate"
> echo $?
> echo "mydate=\"2006-02-29\""
> mydate="2006-02-29"
> date -d "$mydate"
> echo $?
> exit
> [got this back]
> "
> mydate="2006-02-01"
> date: bad conversion
> 1
> mydate="2006-02-29"
> date: bad conversion
> 1
> "
> 
> 
> _______________________________________________
> https://ntlug.org/mailman/listinfo/discuss
> 
> 





More information about the Discuss mailing list