当月和每月的数量天数

时间:2011-04-20 11:37:47

标签: date haskell time

我有两个问题:

我怎样才能获得haskell的当前年份(不是日期)?

我怎样才能在某个月的某个月获得数量天数?例如04.2011已经有30天了。

1 个答案:

答案 0 :(得分:5)

这将为您提供年,月和日:

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 - >诠释。 它需要一年零一个月作为参数,返回天数,就像你需要的那样。 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)
相关问题