timex icon indicating copy to clipboard operation
timex copied to clipboard

Convert string Datetime to milliseconds

Open mvbaffa opened this issue 10 years ago • 2 comments

  • Timex.parse(startDay, "%Y-%m-%d %H:%M:%S", :strftime)
  • startDay = "2016-08-03 00:00:00"

How can I parse and convert the date "2016-08-03 00:00:00" to milliseconds like java.util.Date.getTime() Thanks

mvbaffa avatar Oct 21 '16 04:10 mvbaffa

like so:

iex(1)> startDay = "2016-08-03 00:00:00"                             
"2016-08-03 00:00:00"
iex(2)> date = Timex.parse!(startDay, "%Y-%m-%d %H:%M:%S", :strftime)
~N[2016-08-03 00:00:00]
iex(3)> DateTime.to_unix(Timex.to_datetime(date), :milliseconds)   

note that the result of the parse is a NaiveDateTime. You need to specify a timezone in order to get unix millis from that. The code above assumes UTC. Note that this code works in elixir >= 1.3

jadlr avatar Oct 25 '16 16:10 jadlr

Thanks

Em 25/10/2016 14:20, "Jonas Adler" [email protected] escreveu:

like so:

iex(1)> startDay = "2016-08-03 00:00:00" "2016-08-03 00:00:00" iex(2)> date = Timex.parse!(startDay, "%Y-%m-%d %H:%M:%S", :strftime)~N[2016-08-03 00:00:00] iex(3)> DateTime.to_unix(Timex.to_datetime(date), :milliseconds)

not the the result of the parse is a NaiveDateTime. You need to specify a timezone in order to get unix millis from that. The code above assumes UTC

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bitwalker/timex/issues/227#issuecomment-256084439, or mute the thread https://github.com/notifications/unsubscribe-auth/ABfj5uPmuY0vK6yiQXxwJpw1kR7ZoYecks5q3iwwgaJpZM4Kc2W7 .

mvbaffa avatar Oct 25 '16 18:10 mvbaffa