andrewmcveigh / cljs-time

A clj-time inspired date library for clojurescript.
342 stars 57 forks source link

Time zone is neglected when formatting #23

Open henriklundahl opened 9 years ago

henriklundahl commented 9 years ago

Supplying a time zone to a formatter makes no difference when formatting:

(unparse (formatter "yyyy-MM-dd HH:mm" (time-zone-for-offset 2)) (date-time 2015))
=> 2015-01-01 00:00
(unparse (formatter "yyyy-MM-dd HH:mm" (time-zone-for-offset -2)) (date-time 2015))
=> 2015-01-01 00:00

Same thing when parsing.

andrewmcveigh commented 9 years ago

Thanks for the report!

This is actually not implemented yet, due to JS having very limited timezone support. I think I have a solution though.

henriklundahl commented 9 years ago

OK, I see. I got this to work for converting from JS Date to textual representation in local time:

(unparse (formatter "yyyy-MM-dd HH:mm") (to-default-time-zone dt))

And the other way around:

(js/Date. (.getTime (to-local-date-time
                   (parse (formatter "yyyy-MM-dd HH:mm")
                          dt-text))))
kajism commented 8 years ago

I have just run into this issue and tried to solve it by cljc reader conditionals:

(def formatter-ddMMyyyyHHmm (time-format/formatter "dd.MM.yyyy HH:mm" #?(:clj (time/default-time-zone))))

(defn datetime-to-str [date]
  (if (nil? date)
    ""
    (->> date
         from-date
         #?(:cljs time/to-default-time-zone)
         (time-format/unparse formatter-ddMMyyyyHHmm))))

(defn str-to-datetime [str]
  (when-not (string/blank? str)
    (->> str
         (time-format/parse formatter-ddMMyyyyHHmm)
         #?(:cljs time/from-default-time-zone)
         to-date)))

Unfortunately it stops to work on the clojurescript side when compiled in advanced mode (in dev and simple works fine).