1

I have a date and time input as "2016-09-08" and "3:45 PM". I want to use clojure functions to return them as 2016 09 08 3 45

I have used replace function to remove special characters and reduce to combine. But that becomes a string not an integer. I have tried using formatter function but didn't work.

Can you help me out.

1

1 Answer 1

5

Clojure doesn't have date/time parsing in its "standard library" but there are several options out there. The most popular date/time library is clj-time which is a wrapper around Joda Time (a very popular Java library). Disclaimer: I am one of the current maintainers of clj-time, but it was a popular and mature library long before I got involved.

Add clj-time as a dependency in your project.clj (or build.boot) file, and then you'll need to require the coerce and format namespaces:

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

You'll need to create a "formatter" that matches the syntax of your date and time string when put together. The date part is straight forward (yyyy-MM-dd) but since you want to parse the AM/PM portion in your time part, you need to account for the 12-hour clock there (KK:mm aa):

(def my-date-time (str "2016-09-08" " " "3:45 PM"))
(def my-formatter (tf/formatter "yyyy-MM-dd KK:mm aa"))

Now you can parse your string to a Joda DateTime (in UTC) and then convert it to a Java Date to produce an instant literal in Clojure:

(tc/to-date (tf/parse my-formatter my-date-time))
;=> #inst "2016-09-08T15:45:00.000-00:00"

You can also do this directly via Java interop, using the Java standard library SimpleDateFormat and TimeZone classes:

(def sdf (java.text.SimpleDateFormat. "yyyy-MM-dd KK:mm aa"))
(.setTimeZone sdf (java.util.TimeZone/getTimeZone "UTC"))
(.parse sdf my-date-time)
;=> #inst "2016-09-08T15:45:00.000-00:00"

Using clj-time is generally going to produce much more idiomatic Clojure. For example, in the Java code above you have to create a mutable SimpleDateFormat object and then change its timezone to UTC via a method that returns void. Or rely on doto and Java interop like this:

(.parse (doto (java.text.SimpleDateFormat. "yyyy-MM-dd KK:mm aa")
              (.setTimeZone (java.util.TimeZone/getTimeZone "UTC")))
        my-date-time)
;=> #inst "2016-09-08T15:45:00.000-00:00"

In addition to parsing and formatting, the clj-time library provides conversions to/from several date/time-related types, as well as providing a rich library of date/time arithmetic and comparisons, with support for local date/time as well as UTC.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank for the detailed solution! That was very helpful! I will most likely be asking more questions on Clojure and I would love to continue to ask you if possible!
Sure. Happy to help with Clojure, wherever I can!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.