script
Convert ecto date to another format
I have a date inside Ecto schema yy/mm/dd
date = ~D[2021-07-22]
I want to convert it into this format mm/dd/yy to use it in my template.
date = ~D[07-22-2021]
I have been using Timex, but didn’t figure out which method to use to convert it into this format.
Thanks,
Most Liked
joshdcuneo
If you want the formatted string for your template in the format “month-day-year” there are a few options. You don’t need Timex but if it is already in use in your project there is no reason not to use it I guess. Here are some ways to format a Date struct:
defmodule DateFormatTest do
use ExUnit.Case
@date ~D[2021-07-22]
@formatted_date "07-22-2021"
test "format date with Calendar.strftime" do
assert Calendar.strftime(@date, "%m-%d-%Y") == @formatted_date
end
test "format date with Timex.format" do
assert Timex.format!(@date, "{0M}-{0D}-{YYYY}") == @formatted_date
end
test "format date with Timex.format and :strftime" do
assert Timex.format!(@date, "%m-%d-%Y", :strftime) == @formatted_date
end
end
1
Popular in Questions
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
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
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
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
can someone please explain to me how Enum.reduce works with maps
New
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
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New







