这是我的 Clojurescript 函数,
(defn message-list [messages]
(println messages) ;; stmt#1
[:ul.messages
(for [{:keys [timestamp message name]} @messages]
^{:key timestamp}
[:li
[:time (.toLocaleString timestamp)] ;; stmt#2
[:p message]
[:p " - " name]])])
stmt#1
正在打印,#<Atom: [{:id 1, :name Adeel Ansari, :message Hello, from the other side.,
:timestamp #object[Transit$TaggedValue [TaggedValue: LocalDateTime, 2020-01-13T18:19:50.552]]}]>
和
stmt#2
正在打印,[TaggedValue: LocalDateTime, 2020-01-13T18:19:50.552]
现在,我想将其打印为
13/01/2020 18:19
;我该怎么办? 我不知道如何解码标记值。
最佳答案
您可以从 TaggedValue
中获取该值。使用 .-rep
,然后,您可以解析该 String
使用一些图书馆。
例如,您可以使用 cljc.java-time 解析日期, 像这样:
(let [tv (t/tagged-value "LocalDateTime" "2019-01-01T11:22:33.123")]
(cljc.java-time.local-date-time/parse (.-rep tv))) => #object[LocalDateTime 2019-01-01T11:22:33.123]
或者您可以使用 Tick ;那么你的代码看起来像,
(ns xx.yy.zz
(:require ..
[tick.locale-en-us]
[tick.alpha.api :as t]
..
))
...
(defn message-list [messages]
...
[:li
[:time (t/format (t/formatter "dd/MM/yyyy HH:mm") (t/parse (.-rep timestamp)))]
...]
...)
...
关于json - 如何解码,然后格式化,标记的 LocalDateTime 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59768837/