script

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

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

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
albydarned
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement