我有两个问题:
如何在 haskell 中获取当前年份(不是日期)?
如何在某年某月获得数量天数?例如 04.2011 有 30 天。
最佳答案
这将为您提供年、月和日:
import Data.Time.Clock
import Data.Time.Calendar
date :: IO (Integer,Int,Int) -- :: (year,month,day)
date = getCurrentTime >>= return . toGregorian . utctDay
来源:http://www.haskell.org/haskellwiki/Date
Date.Time.Calendar 中有一个名为 gregorianMonthLength 的函数,其类型为 Integer -> Int -> Int。
它需要一年和一个月作为参数,返回天数,就像你需要的那样。
http://www.haskell.org/ghc/docs/7.0.2/html/libraries/time-1.2.0.3/Data-Time-Calendar.html
一个完整的例子:
import Data.Time.Clock
import Data.Time.Calendar
date :: IO (Integer,Int,Int) -- :: (year,month,day)
date = getCurrentTime >>= return . toGregorian . utctDay
main = do
(year, month, day) <- date
putStrLn $ "Days in this month: " ++ (show $ gregorianMonthLength year month)
关于date - 当前年份和月份中的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5729570/