Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get yesterday's date in Clojure

Tags:

date

clojure

I need to get yesterday's date (not user-based, but relative to the server) in the format YYYY-MM-DD.

What is the best way to achieve this in Clojure?

like image 264
feychu Avatar asked Apr 06 '17 10:04

feychu


2 Answers

With Java 8 Time API:

(import (java.time LocalDate)
        (java.time.format DateTimeFormatter))

(def formatter (DateTimeFormatter/ofPattern "yyyy-MM-dd"))

(def yesterday (.minusDays (LocalDate/now) 1))

(.format formatter yesterday)
;; => "2017-04-05"

Or with Java 8 Time API Clojure wrapper: clojure.java-time

(require '[java-time :as t])

(def yesterday (t/minus (t/local-date)
                        (t/days 1)))

(t/format "yyyy-MM-dd" yesterday)
;; => "2017-04-05"
like image 83
Piotrek Bzdyl Avatar answered Oct 13 '22 00:10

Piotrek Bzdyl


clj-time

(t/minus (t/now) (t/days 1))
like image 23
NielsK Avatar answered Oct 13 '22 00:10

NielsK