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.
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.
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.