andrewmcveigh / cljs-time

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

How can I compare two dates with specified precision? #137

Open khmelevskii opened 5 years ago

khmelevskii commented 5 years ago

Hello.

I need to compare two dates without hours, minutes, seconds. I can do something like this:

(and
  (= (t/year date1) (t/year date2))
  (= (t/month date1) (t/month date2))
  (= (t/day date1) (t/day date2)))

but it looks verbose. it would be great to have function something like this:

(same? date1 date2 :day)
(same? date1 date2 :hour)
(same? date1 date2 :month)
etc
danielcompton commented 5 years ago

Hi @khmelevskii, what are the types of the dates that you are comparing? UtcDateTimes? You could perhaps cast them to midnight and compare them with an equality check as an alternative.

I'm not sure whether a function like what you describe would fit into cljs-time, as its API is meant to be modelled after clj-time. Feel free to write it and drop a link to it here though. Also, if something like this exists in clj-time then I'm happy to revisit this more.