How can I โ€œtrackโ€ a date in a specific time zone?

Using clj-time, I can parse the date and time by doing

(def timestamp (format/parse (formatters :date-time-no-ms) "2013-06-03T23:00:00-0500")) ;=> #<DateTime 2013-06-04T04:00:00.000Z> 

I can convert this back to string by doing

 (unparse (formatters :year-month-day) timestamp) ;=> "2013-06-04" 

This is the year, month, and day of this moment in the UTC time zone. How can I get an unparsed version of DateTime relative to a different time zone? For example, in the above example, I want to specify the UTC-5 time zone and get the string "2013-06-03". I played with from-time-zone and to-time-zone , but didn't seem to find the right combination of functions and arguments.

+7
source share
1 answer

You want to use clj-time.format/with-zone :

 (require '(clj-time [core :as time] [format :as timef])) (timef/unparse (timef/with-zone (:date-time-no-ms timef/formatters) (time/time-zone-for-id "America/Chicago")) (time/now)) ;= "2013-06-02T15:20:03-05:00" 
+8
source

All Articles