在 Clojure 中将纪元时间转换为人类可读的日期

问题描述 投票:0回答:3

我有一个 Unix 纪元时间。如何将其转换为人类可读的日期字符串?我已经检查了 clj-time (https://github.com/clj-time/clj-time),但它似乎只能从可读到纪元。谢谢。

clojure
3个回答
5
投票

您还可以使用原生调用:

(str (java.util.Date. (System/currentTimeMillis)))
;; => "Mon Feb 29 21:59:51 MSK 2016"

2
投票

您可以将其强制为日期时间,然后使用格式化程序(内置或构建您自己的)。

(require '[clj-time.format :as f])
(require '[clj-time.coerce :as c])

(f/unparse  (f/formatters :date-time) (c/from-long 1000000000000))

请注意,

c/from-long
需要几毫秒。


0
投票

对于在 clj 和 cljs 中工作的代码,您可以使用 tick

(require '[tick.core :as tick])
(tick/inst (tick/instant (inst-ms #inst "2023-10-24T11:00:15.251-00:00")))

;; => #inst "2023-10-24T11:00:15.251-00:00"
© www.soinside.com 2019 - 2024. All rights reserved.