electic

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 format a datetime to a string. Can someone point me in the right direction?

Showing Posts 16 to 7

LostKobrakai

LostKobrakai

There‘s also a standalone strftime implemention:
https://github.com/plataformatec/nimble_strftime

Most things calendar did found it‘s way into elixir core in the last few versions, so I tend to suggest it (as well as timex) less and less.

tme_317

tme_317

I would use the Calendar library (mentioned earlier in this thread) with the strftime function to customize the DateTime formats however you like.

kodepett

kodepett

How about a format like 20191101T17:01:58+0000 - I derived this in PHP using Ymd\TH:i:sO format.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you elaborate?

alxvallejo

alxvallejo

How would you format it though?

electic

electic OP

@kujua I went with the Calendar module. The inbuilt one seemed pretty simple and didn’t do diffs, etc. Thanks for everyone’s help!

yurko

yurko

Erlang’s calendar is also good, I use it to get string dates with flexible format like that:

defmodule Util.Time do
  @moduledoc """
  Time helper functions.
  """

  @doc """
  Get current time as string using Erlangs calendar module.
  """
  @spec now_to_string() :: String
  def now_to_string() do
    {{year, month, day}, {hour, minute, second}} = :calendar.local_time()
    "#{day}.#{month |> zero_pad}.#{year} #{hour |> zero_pad}:#{minute |> zero_pad}:#{second |> zero_pad}"
  end

  @spec zero_pad(Integer, Integer) :: String
  defp zero_pad(number, amount \\ 2) do
     number
     |> Integer.to_string
     |> String.rjust(amount, ?0)
  end

end
kujua

kujua

Author of Erlang and Elixir for Imperative Programmers

Sorry for being not clear - was a long evening yesterday :slight_smile:

The mix file has a dependency {:calendar, “~> 0.16.0”}
Starting in the directory where the file resides: iex -S mix. This will load all dependencies.

As @sztosz rightly says this is the library https://github.com/lau/calendar which has more features than the built-in one in 1.3. Also, version 0.16.0 only works with Elixir 1.3, because it accepts the built-in calendar 1.3 types.

sztosz

sztosz

I just realized @kujua were talking about a third party library GitHub - lau/calendar: date-time and time zone handling in Elixir · GitHub, and not about the Elixir’s standard library Calendar http://elixir-lang.org/docs/stable/elixir/Calendar.html. I think he should mention about what library he was writing in his response, to avoid confusion.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews