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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

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
Tee
can someone please explain to me how Enum.reduce works with maps
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
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
_russellb
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
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