shijith.k

shijith.k

Replace time in a DateTime struct

I have a DateTime with me, #DateTime<2019-11-10 13:27:00.0Z> and want to replace the time part of the DateTime struct with another time, say, 11:50:07.00+02:30. The solution I found was to convert the DateTime to string and then replace the time part using String.replace/3.
This is how my code looks.

dt = #DateTime<2019-11-11 13:27:00.0Z>
dt
 |> DateTime.to_string()
 |> String.replace(~r/[[:blank:]][[:alnum:]][[:alnum:]]:[[:alnum:]][[:alnum:]]:[[:alnum:]][[:alnum:]].[[:alnum:]]Z/, "T11:50:07.00+02:30")
 |> DateTime.from_iso8601()

I feel there is a better way to do this, or at least there is a better regex to do it.
Does anyone have any better logic?

Most Liked

mudasobwa

mudasobwa

Creator of Cure

I would go with the direct struct update.

with {:ok, dt, 0} <- DateTime.from_iso8601("2019-11-10 13:27:00.0Z"),
     {:ok, t} <- Time.from_iso8601("11:50:07.00+02:30"),
  do: %DateTime{dt | hour: t.hour, minute: t.minute,
                     second: t.second, microsecond: t.microsecond}
#⇒ ~U[2019-11-10 11:50:07.00Z]

The above would discard the offset, though, and there is no clean way to get the offset in the Time struct because generally speaking the offset [arguably] has a meaning for the dates only. The below would be probable better:

date =
  DateTime.from_iso8601("2019-11-10 13:27:00.0Z")
  |> elem(1)
  |> DateTime.to_date()
  |> Date.to_iso8601()
DateTime.from_iso8601(date <> " 11:50:07.00+02:30")                      
#⇒ {:ok, ~U[2019-11-10 09:20:07.00Z], 9000}

Last Post!

shijith.k

shijith.k

The above would discard the offset

I can’t discard the offset, because after replacing the time, I still need to convert this to UTC.

date =
DateTime.from_iso8601(“2019-11-10 13:27:00.0Z”)
|> elem(1)
|> DateTime.to_date()
|> Date.to_iso8601()
DateTime.from_iso8601(date <> " 11:50:07.00+02:30")
#⇒ {:ok, ~U[2019-11-10 09:20:07.00Z], 9000}

So, for me, this is the suitable option. And its a much better and cleaner code than mine :smile:

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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
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
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
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
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement