1

I want to show date and time in Clojurescript, but anything I tried did not work. Can anyone help with an example? I tried to use JavaScript datatime in different syntax ways, but none of them worked. Thank you.

Update

I tired these but they did not work:

(. (js/Date))
(js/Date.)
(now (js/Date))
(js/Date.new)
(js/Date.new.)
(.new (js/Date)

I just wont to show the current date and time on the screen. Inside a [:h3] tag in the .cljs file.

niloofar
  • 2,244
  • 5
  • 23
  • 44
  • 1
    It's very unclear what you actually want. You write that you tried some things - can you post them? What exactly do you mean by "show date"? If you mean just creating a date as in `new Date()` in JS, it's `(js/Date.)` in CLJS. – Eugene Pakhomov Nov 22 '22 at 05:23
  • Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. – cfrick Nov 22 '22 at 07:44
  • @EugenePakhomov that did not work for me, the page goes blank when I use that. – niloofar Nov 22 '22 at 12:22
  • @cfrick I added the things I tried. The problem is I don't know how to use java introp and did not fond anything helpful by searching. – niloofar Nov 22 '22 at 12:23
  • @EugenePakhomov should [:h3 (js/Date.)] show the date? Or am I doing that wrongly? – niloofar Nov 22 '22 at 12:25
  • If you don't get any other error, does the dev-console give any clues? – cfrick Nov 22 '22 at 13:40
  • What is the expected date (or date + time) format? `[:h3 (.now js/Date)]` returns the number of milliseconds since January 1, 1970, but you probably want to format it somehow. – Martin Půda Nov 22 '22 at 14:09

1 Answers1

2
(.toLocaleString (js/Date.))
;; => "22/11/2022, 17:17:22"

and in [:h3]:

[:h3 (.toLocaleString (js/Date.))]

If you want other formatting you can mix and match the available methods on a Date instance using interop.

user2609980
  • 10,264
  • 15
  • 74
  • 143