timex icon indicating copy to clipboard operation
timex copied to clipboard

New datetime from year, month, day, hour

Open mvbaffa opened this issue 10 years ago • 4 comments

Sorry,

Maybe this is an obviuos answer but I could not find a function to create a datetime from year, month, day, hour.

Something like this: to_datetime(year, month, day, hour, seconds)

Can you help me. Thanks

mvbaffa avatar Nov 03 '16 18:11 mvbaffa

@mvbaffa you can use the standard library functions/macros, for example Timex.to_datetime(NaiveDateTime.new(year, month, day, hour, minutes, seconds), "America/Chicago"), Timex.to_datetime(~N[2016-11-03T12:30:00], "America/Chicago") or Timex.to_datetime({{year, month, day}, {hour, minutes, seconds}}, "America/Chicago"). All three of those produce a DateTime in the "America/Chicago" timezone. You can omit the Timex.to_datetime/2 call if you just want a NaiveDateTime (in the case of the first two examples).

bitwalker avatar Nov 03 '16 18:11 bitwalker

This Worked: sd_upd = NaiveDateTime.new(t.year, t.month, t.day, t.hour, t.minute, 0)

but this: sd_upd = Timex.to_datetime(NaiveDateTime.new(t.year, t.month, t.day, t.hour, t.minute, 0), "Etc/UTC")

Returned:

(FunctionClauseError) no function clause matching in Timex.Protocol.Tuple.to_datetime/2 stacktrace: (timex) lib/datetime/erlang.ex:46: Timex.Protocol.Tuple.to_datetime({:ok, ~N[2016-10-01 03:00:00]}, "Etc/UTC")

mvbaffa avatar Nov 03 '16 18:11 mvbaffa

Ah sorry, I forgot NaiveDateTime.new returns {:ok, NaiveDateTime.t} | {:error, term}, you'd need to extract the NaiveDateTime before passing it to Timex.to_datetime/2

bitwalker avatar Nov 03 '16 19:11 bitwalker

OK thanks

Em 03/11/2016 17:01, "Paul Schoenfelder" [email protected] escreveu:

Ah sorry, I forgot NaiveDateTime.new returns {:ok, NaiveDateTime.t} | {:error, term}, you'd need to extract the NaiveDateTime before passing it to Timex.to_datetime/2

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bitwalker/timex/issues/238#issuecomment-258242078, or mute the thread https://github.com/notifications/unsubscribe-auth/ABfj5rOcro-c7E_7EtXSfumxn7F-4rtOks5q6i-JgaJpZM4Kove8 .

mvbaffa avatar Nov 03 '16 19:11 mvbaffa