If I have this code:
let time = read "2013-02-03 17:00:07.687" :: UTCTime
How can I extract the minutes and seconds components out of the UTCTime
?
A UTCTime
has two components: a day and a DiffTime
. You can get the DiffTime
using utctDayTime
or by pattern matching. From there, you can convert it to a TimeOfDay
using timeToTimeOfDay
. You can then just pattern match against the TimeOfDay
to get the hours, minutes and seconds.
So you could do this:
let TimeOfDay hours minutes seconds = timeToTimeOfDay (utctDayTime time)
You can also use the todMin
and todSec
functions to get the minutes and seconds respectively out of the TimeOfDay
.
You can use the time-lens package, which makes time and date manipulation much easier. E.g.
> let time = read "2013-02-03 17:00:07.687" :: UTCTime
> getL seconds time
7.687000000000
If you import Data.Lens.Common
, there's also an infix version of getL
:
> time ^. minutes
0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With