Problem:
Using haskell, you want to get the current year, month and day (for the UTC time zone) as integral values.
Solution:
You need the time package for this task. Use cabal install time
to install.
Our code is similar to this HaskellWiki entry, however it provides a standalone runnable program (use runghc <filename>.hs
to execute) which is more readable for beginners.
UTC time:
Note that the UTC time might differ from your local time depending on your timezone.
import Data.Time.Clock import Data.Time.Calendar main = do now <- getCurrentTime let (year, month, day) = toGregorian $ utctDay now putStrLn $ "Year: " ++ show year putStrLn $ "Month: " ++ show month putStrLn $ "Day: " ++ show day
Local time:
It is also possible to get your current local time using your system’s default timezone:
import Data.Time.Clock import Data.Time.Calendar import Data.Time.LocalTime main = do now <- getCurrentTime timezone <- getCurrentTimeZone let zoneNow = utcToLocalTime timezone now let (year, month, day) = toGregorian $ localDay zoneNow putStrLn $ "Year: " ++ show year putStrLn $ "Month: " ++ show month putStrLn $ "Day: " ++ show day
Daylight saving time is also taken into account using this method.