Is it possible to find out the date format the machine is using in Elixir? i.e. dd/mm/yy or mm/dd/yy so we can format a date string accordingly?
Asked
Active
Viewed 744 times
0
-
Machines do not use any specific “date format,” you might want to ask internets about `LC_TIME` environment variable though. – Aleksei Matiushkin May 16 '19 at 15:03
-
In Javascript https://stackoverflow.com/questions/2388115/get-locale-short-date-format-using-javascript#answer-9893752 - all you need is the environment var of `LC_TIME` using `System.get_env(varname)` and then adapt the that Js array. – GavinBrelstaff May 16 '19 at 15:21
-
1@GavinBrelstaff I looked at using LC_TIME. If I execute locale it gives me LC_TIME="en_GB.UTF-8" which is right. I can look this up to get the dd-mm-yyyy format in the javascript arrays. If I change my date format to mm-dd-yy on the computer then LC_TIME does not change, so it's close but it is still not really the date format. – iphaaw May 17 '19 at 13:29
-
@iphaaw when you change your date format what way do you do it? Is it on windows, mac, linux? – GavinBrelstaff May 17 '19 at 15:15
-
I was changing it on Mac through the system preferences. We primarily need it to be on Windows but Mac and Linux will be useful too. Thanks. – iphaaw May 17 '19 at 16:20
-
It could be the shell from which you launch Elixir is only informed of LC_TIME on start up and its env vars don't get informed of any subsequent changes. In that case you might need to launch a new shell from within Elixir and then somehow scrape its env vars. But this is just my guess. – GavinBrelstaff May 18 '19 at 05:59
2 Answers
1
In "core Elixir" there is no such functionality, as Elixir/Erlang do not ship with locales data nor it provides API to use system data. Instead you need to fetch CLDR data on your own and use that, fortunately there is ex_cldr
library that does that for you. In addition there is extension ex_cldr_date_times
that supports formatting dates. So in the end, when you install both of these libraries you can use:
Cldr.DateTime.to_string(DateTime.utc_now)
To receive localized string in current locale.

Hauleth
- 22,873
- 4
- 61
- 112
-1
You can use DateTime.utc_now
and then format date you want:
iex(5)> d = DateTime.utc_now
#DateTime<2019-05-16 15:01:51.662814Z>
iex(6)> DateTime.to_string(d)
"2019-05-16 15:01:51.662814Z"
iex(7)> s = "#{d.year}/#{d.month}/#{d.day}"
"2019/5/16"
Or you can get the data format from :calendar.local_time
like below and then print it into everything you want:
iex(12)> {{y, m, d},_} = :calendar.local_time()
{{2019, 5, 16}, {22, 4, 29}}
iex(13)> y
2019
iex(14)> m
5
iex(15)> d
16

bxdoan
- 1,349
- 8
- 22
-
@bxdoan Unfortunately this was not the question I asked. I can get the day, month year. I just need to know if it should be displayed in dd/mmyy or mm/dd/yy format! – iphaaw May 17 '19 at 13:11