user2760913
user2760913

Reputation: 65

UNIX time stamp to iso8601 in ruby

I am trying to convert a UNIX timestamp into iso8601 format.

for ex:

UNIX timestamp "1483050457000" 

how do I convert it to iso8601 time format in ruby ??

Upvotes: 2

Views: 1542

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110755

require 'time'
Time.at("1483050457000".to_i).iso8601
  #=> "48965-12-30T12:16:40-08:00"

require 'time' is essential here. Without it you find that you only have access to Time's core methods. Run Time.instance_methods.sort to list them. By first executing require 'time' the following instance methods are added to the class Time: => [:httpdate, :iso8601, :rfc2822, :rfc822, :to_date, :to_datetime, :to_time, :xmlschema].

Upvotes: 3

MarsAtomic
MarsAtomic

Reputation: 10696

You seem to be getting your epoch time stamp in the form of a string, so the following will probably be easiest for you:

DateTime.strptime("1483050457000",'%s')

Note: You seem to have supplied an invalid time stamp in your question, otherwise, I'd have included the output. Try that code with a valid time stamp, and you should get what you want.

Upvotes: 2

Related Questions