[racket] date format conversion

From: prad (prad at towardsfreedom.com)
Date: Sat Feb 12 20:54:35 EST 2011

Neil Van Dyke <neil at neilvandyke.org> writes:

> prad wrote at 02/12/2011 08:17 PM:
>> is there a function which will convert
>> "February 12, 2011"
>> to
>> "2011-02-12"
>
> Racket search turns up "string->date" and "date->string".  You can use
> them to define your new procedure like this...
>
> ;;;; BEGIN
>
> #lang racket/base
>
> (require srfi/19)
>
> (define (us-long-date-str->iso-8601-date-str long-date-str)
>  (date->string (string->date long-date-str
>                              "~B ~d, ~Y")
>                "~Y-~m-~d"))

thx neil for the superfast reply!
i just figured it out a few minutes ago and was coming to report my
findings. :D

here's what i did:

(date->string (string->date "February 12, 2011" "~B ~d, ~Y") "~Y-~m-~d")

but i'll turn it into a function as you have done, of course.


this stuff below is a new encounter:

> (require test-engine/racket-tests)
> (check-expect (us-long-date-str->iso-8601-date-str "February 12,
> 2011") "2011-02-12")
> (check-expect (us-long-date-str->iso-8601-date-str "February 09,
> 2011") "2011-02-09")
> (check-expect (us-long-date-str->iso-8601-date-str "February 9, 2011")
> "2011-02-09")
> (test)

my son helped me understand what it was doing after we looked
check-expect up.  so this runs a function for you and checks to see that
the result is really what you say it is going to be!

that's pretty useful, so a second thx!


-- 
in friendship,
prad



Posted on the users mailing list.