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
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}
2
Popular in Questions
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
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
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
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
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
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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 am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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
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
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New








