Elixir datetime from string date

Code examples

2
0

elixir string to datetime

defmodule SomeModule do
  @doc """
      iex> SomeModule.to_date("2020-01-30")
      ~D[2020-01-30]
  """
  def to_date(string), do: Date.from_iso8601!(string)

  @doc """
      iex> SomeModule.to_date_time("1997-07-16T19:20:30.45Z")
      ~U[1997-07-16 19:20:30.45Z]
  """
  def to_date_time(string) do
    case DateTime.from_iso8601(string) do
      {:ok, date_time, _offset} -> date_time
      error -> error
    end
  end
end

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................