How to test a String if it is a valid date?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to test a String if it is a valid date?

Joseph Alotta
Greetings,

I want to test a String to see if it can be a Date using mm/dd/yyyy format.  I want something that won’t cause a walkback window.  Is there a String method isValidAsADate?

Sincerely,

Joe.



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to test a String if it is a valid date?

Dan Norton
On 29 Mar 2016 at 19:07, Joseph Alotta wrote:

> Greetings,
>
> I want to test a String to see if it can be a Date using mm/dd/yyyy
> format.  I want something that won´t cause a walkback window.  Is
> there a String method isValidAsADate?
>
> Sincerely,
>
> Joe.
>

You might have a method like this:

isDate: aString
        "Answer whether aString is in the form 'mm/dd/yyyy' "
        | w x |
        w := aString asString select: [:a | a isDigit or: a = $/].
        x := w findTokens: '/'.
        ^ x size = 3 and: [w  = aString]


This is the general idea. Let me know if you have any questions.

 - Dan
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: How to test a String if it is a valid date?

Ron Teitelbaum
In reply to this post by Joseph Alotta
Hi Joe,

You could just handle the error.

['1/22/2016' asDate class = Date ] on: Error do: [:ex | ^false]
returns true

['15/22/2016' asDate class = Date ] on: Error do: [:ex | ^false]
returns false

Or even

[myDate := '1/22/2016' asDate] on: Error do: [:ex | ^nil].

Then you can just check if myDate isNil.

All the best,

Ron Teitelbaum
Head Of Engineering
3D Immersive Collaboration Consulting
[hidden email]
Follow Me On Twitter: @RonTeitelbaum
www.3Dicc.com
https://www.google.com/+3Dicc

> -----Original Message-----
> From: [hidden email] [mailto:beginners-
> [hidden email]] On Behalf Of Joseph Alotta
> Sent: Tuesday, March 29, 2016 8:07 PM
> To: [hidden email]
> Subject: [Newbies] How to test a String if it is a valid date?
>
> Greetings,
>
> I want to test a String to see if it can be a Date using mm/dd/yyyy format.  I
> want something that won’t cause a walkback window.  Is there a String
> method isValidAsADate?
>
> Sincerely,
>
> Joe.
>
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners