Haskell function to get part of a date as a string

I have a question about the dates of beginning and Stringin Haskell.

I need to get a part of the date (year, month or day) as Stringin Haskell. I found out that if I write the following two lines in GHCi

Prelude> now <- getCurrentTime
Prelude> let mon = formatTime defaultTimeLocale "%B" now

that monhas a type String. However, I cannot include this in a function. I tried for example the following:

getCurrMonth = do
    now <- getCurrentTime
    putStrLn (formatTime defaultTimeLocale "%B" now)

But this returns a type IO (), and I need String(also not IO String, only String).

I understand that the operator docreates a monad that I do not want, but I could not find any other solution to get the date in Haskell.

So, is there a way to write such a function?

Thanks in advance for your help!

+5
4

, , IO, !

, IO:

> getCurrMonth :: IO String
> getCurrMonth = do
>    now <- getCurrentTime
>    return (formatTime defaultTimeLocale "%B" now)

(, ) :

> main = do
>     s <- getCurrMonth
>     ... do something with s ...
+10

, .

import System.Locale (defaultTimeLocale)
import System.Time (formatCalendarTime, toUTCTime, getClockTime, ClockTime)

main = do now <- getClockTime
          putStrLn $ getMonthString now

getMonthString :: ClockTime -> String
getMonthString = formatCalendarTime defaultTimeLocale "%B" . toUTCTime

, getMonthString , IO getClockTime .

old-time, , , -, .:( , , toUTCTime.

+5

, . , Haskell - , , . Haskell.org , , , . , , , > - Haskell, . , , . , , .

, unsafePerformIO. , "", . .

Haskell!

+2

, -. , getCurrMonth , , IO.

+1

All Articles