joaothallis

joaothallis

Best way to change a hour of a DateTime

I has a DateTime with a tz different of UTC and I need to update your hour, I used Map.put to this: Map.put dt, : hour, 5. Is there a best way to do this using DateTime or Timex

Marked As Solved

NobbZ

NobbZ

The DateTime struct is documented with its fields, therefore its totally fine to use Map.put/3 or even the update syntax (%{dt | hour: 5}).

Also, because of the public definition of the struct, its totally fine for you to access the fields directly and to change them. You probably won’t find functions that abstract this away.

Also Liked

LostKobrakai

LostKobrakai

Just be aware that this might result in an invalid datetime. Depending on the timezone there might be gaps or overlaps.

entone

entone

If your logic allows it, it might be worth looking at the add function of the DateTime module.

polvalente

polvalente

Nx Core Team
date = ~D[2005-04-05]
time = ~T[08:30:00]

with_timezone = date |> Date.to_erl |> Timex.DateTime.Helpers.construct("America/Sao_Paulo") 
with_timezone_and_time = Timex.add(with_timezone, Timex.Duration.from_time(time))

This isn’t as simple as using Map.put or the %{struct | field: val} syntax, but perhaps makes things more clear.

Another possibility still:

date = ~D[2005-04-05]
time = ~T[08:30:00]
{:ok, naive} = NaiveDateTime.new(date, time)
{:ok, with_timezone_and_time} = DateTime.from_naive(naive, "America/Sao_Paulo")

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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
clayschick
I'm trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of uniqu...
New
siddhant3030
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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

We're in Beta

About us Mission Statement