How to get the current time with GHCJS? Should i try to access Date
or use Haskell base libraries? Is there an utility function somewhere in the GHCJS base libraries?

- 11,511
- 8
- 40
- 47
-
The haskell libraries seem to work - any reason not to use them? – ondra Dec 02 '15 at 21:32
-
I think that i did not find a way to use them. Would you write an answer here about your idea? I will try it and mark it as the correct answer if it will work – danza Dec 04 '15 at 10:29
2 Answers
The Data.Time.Clock
module seems to work well:
import Data.Time.Clock (getCurrentTime)
import Data.Time.Format -- Show instance
main = do
now <- getCurrentTime
print now

- 9,122
- 1
- 25
- 34
-
Yes, this works, and it is way cleaner, thanks! This introduces a dependency on `time` – danza Dec 07 '15 at 18:08
The solution i found currently is quite ugly, but it works for me, so maybe it can save some time to somebody:
{-# LANGUAGE JavaScriptFFI #-}
import GHCJS.Types( JSVal )
import GHCJS.Prim( fromJSString )
foreign import javascript unsafe "Date.now()+''" dateNow :: IO (JSVal)
asInteger = read (fromJSString dateNow) :: Integer -- this happens in IO
The ugliness comes from not finding a JSInteger
type in GHCJS, which would be needed in order to get the result of Date.now()
which is a long integer. So i need to produce a string concatenating a string to the result of Date.now()
in Javascript. At this point i could get a JSString
as result, but that would not be an instance of Read
so using read
would not work. So i get a JSValue
and convert it to String
using fromJSString
.
Eventually there might be a JSInteger
in GHCJS, or JSString
might become an instance of Read
, so if you are reading this from the future try out something more elegant!

- 11,511
- 8
- 40
- 47