Extracting a Date and a Time out of a DateTime
let (DateTime myDate myTime) = myDateTime
date :: DateTime -> Date time :: DateTime -> Time
Adjusting a DateTime
modifyDate :: (Date -> Date) -> DateTime -> DateTime modifyTime :: (Time -> Time) -> DateTime -> DateTime modifyDateF :: forall f. Functor f => (Date -> f Date) -> DateTime -> f DateTime modifyTimeF :: forall f. Functor f => (Time -> f Time) -> DateTime -> f DateTime
All these functions take as the first argument a function that modifies the specific
DateTime component, a
DateTime and return the new value.
The latter two are convenience functions which let us work with
modifying functions that produce a Functor.
This is very useful, for example, in conjunction with the adjust function which produces a
(which is a
addThreeDays :: Date -> Maybe Date addThreeDays = adjust (Days 3.0) oldDateTime :: DateTime newDateTime :: Maybe DateTime newDateTime = modifyDateF addThreeDays oldDateTime
It’s also possible to use adjust directly:
adjust :: forall d. Duration d => d -> DateTime -> Maybe DateTime
Difference between two DateTime values
diff :: forall d. Duration d => DateTime -> DateTime -> d
The result of
diff is one of the types (Days,
Seconds, Milliseconds) which implements the
Duration type class. We need to help the compiler
by specifying the type when it’s not clear from the context.
daysDiff :: Days daysDiff = diff date1 date2 hoursDiff :: Hours hoursDiff = diff date1 date2