bgpratt2000

bgpratt2000

How to convert a Date struct to an easily readable string?

Hi!

My goal is to convert the Date struct to an easily-readable string version of that date.

For example:

~D[2000-01-15] would be converted to “January 15th, 2024”.

Does anyone know of a function or library that already does this?

Thanks!!!

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The function you’re looking for is this one: Calendar.strftime/3

That will let you pass in the date and then a format string in which you can specify things like the full name of the month, date, etc.

mudasobwa

mudasobwa

Creator of Cure

Nah :slight_smile: The below is the real overusing of Elixir killer features:

defmodule DayEnding do
  Enum.each(1..31, fn
    day when div(day, 10) == 1 -> def ending(unquote(day)), do: "th"
    day when rem(day, 10) == 1 -> def ending(unquote(day)), do: "st"
    day when rem(day, 10) == 2 -> def ending(unquote(day)), do: "nd"
    day when rem(day, 10) == 3 -> def ending(unquote(day)), do: "rd"
    day -> def ending(unquote(day)), do: "th"
  end)
end
ibarch

ibarch

It could be implemented somewhere in Cldr.Calendars or Cldr.Numbers. If it isn’t, or if you’re not willing to add an extra dependency, writing your own implementation is straightforward:

def ending(day) when day in [1, 21, 31], do: "st"
def ending(day) when day in [2, 22], do: "nd"
def ending(day) when day in [3, 23], do: "rd"
def ending(_), do: "th"

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

We're in Beta

About us Mission Statement