Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I go about converting this time string to epoch time in ruby?

Tags:

time

ruby

How would I go about converting 2012-12-13T14:43:35.371Z to epoch time?

One approach I was considering was to strip the T and Z and then try to use the DateTime library to find a way to convert to epoch. Before I go down that path, I was curious if there is a gem I could use to do this quickly. On a related note, I will be doing time calculations after I convert to epoch.

like image 860
moorecats Avatar asked Dec 12 '22 18:12

moorecats


1 Answers

For example:

require 'date'

# as an integer:
DateTime.parse('2012-12-13T14:43:35.371Z').to_time.to_i
# or as a string:
DateTime.parse('2012-12-13T14:43:35.371Z').strftime('%s')
like image 186
rausch Avatar answered Dec 14 '22 23:12

rausch