[CLJS] Finding current time in millis

892 views
Skip to first unread message

Shantanu Kumar

unread,
Jan 14, 2012, 4:49:03 AM1/14/12
to Clojure
Hi,

I am trying to write a CLJS (0df1bc4d7b) function that finds out the
current system time in milliseconds, equivalent of (System/
currentTimeMillis) in Clojure:

(ns foo)

(defn now []
;; new Date().getTime()
(.getTime (js/Date.)))


This displays the following when displayed using JavaScript
alert("Found " + foo.now()):

Found function getTime() {
[native code]
}


When I wrap into a function call
(defn now []
;; new Date().getTime()
((.getTime (js/Date.))))

I see the exception in Firebug: "Can't convert null to object" while
executing the following code:
return(new Date).getTime.call(null)

What am I doing wrong?

Shantanu

Baishampayan Ghose

unread,
Jan 14, 2012, 4:58:48 AM1/14/12
to clo...@googlegroups.com

.getTime is a method which takes no arguments. In JavaScript (and by
extension ClojureScript) instance methods are also properties.

So in your case, (.getTime (js/Date.)) actually returns the value of
the method (the native code) instead of calling it.

If you wish to call .getTime instead, you'll have to invoke it thusly
- (. (js/Date.) (getTime))

Also note that this problem has been fixed in a very recent commit
with the introduction of a new property lookup syntax -
http://dev.clojure.org/display/design/Unified+ClojureScript+and+Clojure+field+access+syntax

Hope this helps.

Regards,
BG

--
Baishampayan Ghose
b.ghose at gmail.com

Shantanu Kumar

unread,
Jan 14, 2012, 5:24:54 AM1/14/12
to Clojure
> If you wish to call .getTime instead, you'll have to invoke it thusly
> - (. (js/Date.) (getTime))

It works. Thank you!

Regards,
Shantanu
Reply all
Reply to author
Forward
0 new messages