juxt

Joe 2020-06-18T17:27:54.175Z

I'm using this function with tick to return the 'last day of the month 3 months from now' - is there a more elegant way to do this?

(defn next-period-date [date]
  (str
    (t/- (apply t/new-date
                (let [new-month (+ 1 (mod (+ 3 (t/int (t/month date))) 12))]
                  [(if (< new-month 5)
                     (inc (t/int (t/year date)))
                     (t/int (t/year date)))
                   new-month
                   1]))
         (t/new-period 1 :days))))

2020-06-18T18:47:25.175600Z

I have first-of-month and last-of-month helpers - then i'd combine that with (t/new-period 3 :months)

2020-06-18T18:48:01.176200Z

(defn first-day-of-month
  ([] (first-day-of-month (t/today)))
  ([date] (-> (t/new-date (int-year date) (int-month date) 1)
            (t/at (t/midnight)))))

(defn last-day-of-month
  "date - tick/date or similar"
  ([] (last-day-of-month (t/today)))
  ([date]
   (let [the-first (first-day-of-month date)]
     (t/end (t/bounds the-first
              (t/- (t/+ the-first (t/new-period 1 :months))
                (t/new-period 1 :days)))))))

(last-day-of-month (t/+ (t/today) (t/new-period 3 :months)))

tomd 2020-06-18T20:50:33.178200Z

@allaboutthatmace1789 I think you want to be looking at t/year-month like so:

(defn next-period-date [date]
  (-> (t/+ date (t/new-period 3 :months))
      t/year-month
      t/end
      (t/- (t/new-period 1 :days))
      t/date))
it's a bit annoying that we need to subtract a day as the penultimate step, but then I guess the end of a month is technically considered to be in the next month 🤷

tomd 2020-06-18T21:00:08.178800Z

@danvingo fyi that means your first-day-of-month fn can be shortened to this:

(def first-day-of-month (comp t/date t/beginning t/year-month))

2020-06-18T21:49:20.179100Z

very nice, thanks @tomd

🙂 1
Joe 2020-06-18T22:51:35.179600Z

Nice, thanks both!