json - 如何解码,然后格式化,标记的 LocalDateTime 值

标签 json clojure clojurescript reagent transit

这是我的 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/

相关文章:

c# - 从 C# 发送的 JSON 但未在 php 中作为 JSON 接收

python - Json 循环的列表索引超出范围错误

javascript - 如何将 JSON 数组添加到 javascript 中的每个 JSON 对象?

Clojure、奎尔 : Printing in draw function

clojure - 如何在重新框架中遍历订阅的集合并将数据显示为列表项?

ruby-on-rails - 在仅限 Rails 5 API 的应用程序中使用 Ransack

clojure - 我需要帮助破译这个 clojure 代码

clojure - 为 Clojure 协议(protocol)提供多种实现

macos - 如果出现错误 : "Could not locate clojure/data/files" appears?,如何在 macOS Monterey 12.5 上安装 Dirac(ClojureScript 的 Chrome DevTools 分支)

clojurescript 断点