DEV Community

Clarice Bouwer
Clarice Bouwer

Posted on

How to mask a dd/mm/yyyy date string in ClojureScript

(defn mask-date [date]
  (let [date (-> date
                 (clojure.string/replace #"/" "")
                 (clojure.string/replace #"^(\d{2})(\d{1,})$" "$1/$2")
                 (clojure.string/replace #"^([\d/]{0,5})(\d{0,})$" "$1/$2")
                 (.substring 0 10))]
    (if (.endsWith date "/")
      (.substring date 0 (- (count date) 1))
      date)))
Enter fullscreen mode Exit fullscreen mode

This function can be used in a reactive app to mask the date value on the fly while typing.

Top comments (0)