HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrubyCritical

How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howdatetimetimestampsincerubyconvertepochsecondsunix

Problem

How do you convert a Unix timestamp (seconds since epoch) to Ruby DateTime?

Solution

DateTime.strptime can handle seconds since epoch. The number must be converted to a string:

require 'date'
DateTime.strptime("1318996912",'%s')

Code Snippets

require 'date'
DateTime.strptime("1318996912",'%s')

Context

Stack Overflow Q#7816365, score: 400

Revisions (0)

No revisions yet.