Is there a way in Haskell to parse a date from a string of format "YYYY-MM-DD"? That is, something like parseDateFromString 2016-10-20 that would then return a date in some commonly used Haskell date format.
1 Answer
Data.Time provides parseTimeM, which is a bit unwieldy. I might be overlooking a simpler alternative.
> import Data.Time
> parseTimeM False defaultTimeLocale "%Y-%m-%d" "2016-10-20" :: Maybe UniversalTime
Just 2016-10-20 00:00:00
The return type is fairly open. You can return the value to any monad; Maybe seemed like a good example to reflect an answer or a failure to parse the string. The type wrapped by Maybe is any instance of the ParseTime typeclass; UniversalTime is defined in the library.