A
A
AshotBes2018-10-01 22:06:12
Haskell
AshotBes, 2018-10-01 22:06:12

How to subtract UTCTIme from UTCTime and get the difference in days?

H:p:// I want to subtract UTCTime from UTCTime. I found this function:
diffUTCTime :: UTCTime -> UTCTime -> NominalDiffTime
But I get the difference in NominalDiffTime, and I need to get the difference in days
Is there any function for that?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
youngmysteriouslight, 2018-10-01
@youngmysteriouslight

The most frontal way is to use diffTimeToPicoseconds and then divide by some factor equal to the number of picoseconds in a day.

A
AshotBes, 2018-10-02
@AshotBes

NominalDiffTime is, among others, a Fractional, so you can divide your result with nominalDay:
Prelude Data.Time.Clock> myDiffTime / nominalDay
2.314814814814s
You can use its RealFrac instance to round, floor, etc. it:
Prelude Data.Time.Clock > round $ myDiffTime / nominalDay
2
Prelude Data.Time.Clock > truncate $ myDiffTime / nominalDay
2
Prelude Data.Time.Clock > floor $ myDiffTime / nominalDay
2
Prelude Data.Time.Clock > ceiling $ myDiffTime / nominalDay
3

L
lo-fi, 2018-10-05
@hrls

Yes, there is, diffDays .

import Data.Time.Clock (utctDay, getCurrentTime)
import Data.Time.Calendar (diffDays)

main = do
    let epoch = read "1970-01-01 00:00:00 UTC"
    now <- getCurrentTime
    print $ (utctDay now) `diffDays` (utctDay epoch)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question