Search code examples
datetimeclojureedn

How can I convert "DD/MM/YYYY HH:MM:SS" to a clojure inst value?


Google Forms gives me this :

"02/12/2014 10:44:36"

What is a good way for converting it to edn time format using clojure?

;=> #inst "2014-12-02T10:44:36.000-00:00"

Thank you!

UPDATE:

GForm is giving me hours from 0 to 23. So, the answer should work with this:

"02/12/2014 15:44:36"

Thank you!


Solution

  • You can use clj-time:

    (require '[clj-time.coerce :as c]
             '[clj-time.format :as f])
    
    (def custom-formatter (f/formatter "dd/MM/yyyy hh:mm:ss"))
    
    (pr-str
     (c/to-date
      (f/parse custom-formatter "02/12/2014 10:44:36")))
    
    ; => "#inst \"2014-12-02T10:44:36.000-00:00\""