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 would like to know what is the best IDE for elixir development?
New
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
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
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 have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
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
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
Other popular topics
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
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
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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







