Twistleton
Twistleton

Reputation: 2935

How get yesterday in Haskell? Current date - 1 day?

How get yesterday in Haskell? For the current day - of course it works like this:

date :: IO (Integer,Int,Int) -- :: (year,month,day)
date = getCurrentTime >>= return . toGregorian . utctDay

But for yesterday? Does that work with diffUTCTime?

With sql:

select current date, current date - 1 day from sysdba.routine 
08.11.2018 07.11.2018

But with Haskell?

Upvotes: 4

Views: 531

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

You can use the addUTCTime :: NominalDiffTime -> UTCTime -> UTCTime function with as difference -nominalDay. For example:

Prelude Data.Time.Clock> fmap (addUTCTime (-nominalDay)) getCurrentTime
2018-11-07 15:27:57.8510597 UTC

or when you want to obtain the Gregorian date:

Prelude Data.Time.Clock Data.Time.Calendar> fmap (toGregorian . utctDay . addUTCTime (-nominalDay)) getCurrentTime
(2018,11,7) 

Upvotes: 6

Related Questions