Anton Harald
Anton Harald

Reputation: 5924

unparse js Date to local time string via cljs-time

it's 15:51 here.

Well, in UTF it's 13:51. I'm trying to unparse the current local hours by the help of the cljs-time library.

Here's the non-local approach:

(require [cljs-time.format :as tf]
         [cljs-time.coerce :as tc])

(tf/unparse (tf/formatter "HH") (tc/from-date (js/Date.)))
;; 13 

Unfortunately the following produces the same result and not the desired 15:

(tf/unparse-local (tf/formatter-local "HH") (tc/from-date (js/Date.)))

Does anybody know, what's going on here?

Upvotes: 1

Views: 1437

Answers (1)

Aleph Aleph
Aleph Aleph

Reputation: 5395

By default, cljs-time works via goog.date.UtcDateTime, which returns UTC hours and minutes.

unparse-local and formatter-local just remove the time zone field from the format string. They do not affect the time zone.

To work with the local (default) time, goog.date.DateTime, you can use cljs-time.core/to-default-time-zone:

(require '[cljs-time.core :as time]
         '[cljs-time.format :as fmt])

(tf/unparse (tf/formatter "HH") (time/to-default-time-zone (js/Date.)))

This should return your local time in hours.

Upvotes: 2

Related Questions