简体   繁体   中英

How to compare two UTCTime values and get the difference in days?

I want to take away from UTCTime - UTCTime. I found the following 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 this?

Something other than nominalDiffTimeToSeconds ?!

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM