Skip to content

Commit 5eb0852

Browse files
committed
Use date format inferring from browser to decide formatter
1 parent 2367d35 commit 5eb0852

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

‎project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject org.clojars.intception/om-widgets "0.3.48"
1+
(defproject org.clojars.intception/om-widgets "0.3.49"
22
:description "Widgets for OM/React"
33
:url "https://github.com/orgs/intception/"
44
:license {:name "Eclipse Public License"

‎src/om_widgets/textinput.cljs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,31 @@
55
[om-widgets.utils :as utils]
66
[cljs-time.format :as time-format]
77
[cljs-time.coerce :as timec]
8+
[goog.object :as gobj]
89
[pallet.thread-expr :as th]))
910

1011

1112
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12-
(def date-local-format "dd/MM/yyyy")
1313
(def date-local-mask "00/00/0000")
1414
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1515

16+
(defn get-browser-locale
17+
[]
18+
(or (gobj/get js/navigator "language")
19+
(first (gobj/get js/navigator "languages"))
20+
"en")) ;; Default to English if no locale is found
21+
22+
(defn infer-date-format-pattern
23+
[]
24+
(let [locale (get-browser-locale)
25+
test-date (js/Date. 2024 10 28) ; Nov 28, 2024 (months are zero-based)
26+
formatter (js/Intl.DateTimeFormat. locale)
27+
formatted-date (.format formatter test-date)]
28+
(-> formatted-date
29+
(clojure.string/replace #"2024" "yyyy")
30+
(clojure.string/replace #"11" "MM")
31+
(clojure.string/replace #"28" "dd"))))
32+
1633
(defn- date-from-localstring
1734
[value fmt]
1835
(let [d (time-format/parse (time-format/formatter fmt) value)]
@@ -27,7 +44,7 @@
2744
[input-type value]
2845
(condp = input-type
2946
"date" (try
30-
(string-from-date value date-local-format)
47+
(string-from-date value (infer-date-format-pattern))
3148
(catch js/Error e
3249
;; assume empty string for unhandled values
3350
(str value)))
@@ -37,7 +54,7 @@
3754
[output-type value]
3855
(condp = output-type
3956
"date" (try
40-
(date-from-localstring value date-local-format)
57+
(date-from-localstring value (infer-date-format-pattern))
4158
(catch js/Error e
4259
value))
4360
"numeric" (let [f (js/parseFloat value)]

0 commit comments

Comments
 (0)