Created
November 25, 2014 21:20
-
-
Save jasonneylon/a0ee68dee0284d3fe13b to your computer and use it in GitHub Desktop.
ClojureScript Date parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#+clj (defn parse-date | |
[date-string] | |
(f/parse (f/formatter "dd/MM/yyyy") date-string)) | |
;; Javascript date uglyness: http://stackoverflow.com/questions/2587345/javascript-date-parse | |
#+cljs (defn parse-date | |
"Parses a UK date string such as 29/01/2014 and converts it into a JS date object or nil if invalid. | |
Handles bizarro javascript case such as new Date(2014, 1, 31) => Mon Mar 03 2014 00:00:00 GMT+0000 (GMT)" | |
[date-string] | |
(let [date-parts (clojure.string/split date-string #"/") | |
month (dec (js/parseInt (second date-parts) 10)) | |
year (nth date-parts 2 nil) | |
date (js/Date. year month (first date-parts))] | |
(when (and year (not (js/isNaN (.getTime date))) (= month (.getMonth date))) | |
date))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment