Open In App

How to Convert a UNIX Timestamp (Seconds Since Epoch) to Ruby DateTime?

Last Updated : 21 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Epoch time, also known as Unix time or POSIX time, is a way of representing time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Converting epoch time to a human-readable date and time is a common task in programming, especially in Ruby. This article focuses on discussing different methods to convert a UNIX timestamp to Ruby datetime.

Using Time Class and DateTime Class

In this method, we first convert the Unix timestamp to a Time object using Time.at.Then, it converts the Time object to a DateTime object using DateTime.parse.

Below is the Ruby program to convert a UNIX timestamp (seconds since epoch) to Ruby DateTime using the Time class and DateTime class:

Ruby
# Ruby program to convert a UNIX timestamp 
# (seconds since epoch) to Ruby DateTime
require 'date'

def timestamp_to_datetime_method1(timestamp)
  
  # Convert the timestamp to a Time object
  time = Time.at(timestamp)
  
  # Convert the Time object to a DateTime object
  DateTime.parse(time.to_s)
end

# Example usage
timestamp = 1625097600
datetime = timestamp_to_datetime_method1(timestamp)
puts datetime 

Output:

Using Time Class and DateTime Class
Using Time Class and DateTime Class

Using DateTime.strptime

In this method we use DateTime.strptime to parse the Unix timestamp where the format '%s' specifies that the input is a Unix timestamp.

Below is the Ruby program to convert a unix timestamp (seconds since epoch) to Ruby DateTime using DateTime.strptime:

Ruby
# Ruby program to convert a unix timestamp 
# (seconds since epoch) to Ruby DateTime
require 'date'

def timestamp_to_datetime_method2(timestamp)
  # Convert the timestamp directly to a DateTime object using strptime
  DateTime.strptime(timestamp.to_s, '%s')
end

# Example usage
timestamp = 1625097600
datetime = timestamp_to_datetime_method2(timestamp)
puts datetime  # Output: 2021-07-01T00:00:00+00:00

Output:

Using DateTime.strptime
Using DateTime.strptime

Using Time#to_datetime

In this method we first convert the Unix timestamp to a Time object using Time.at and than use #to_datetime to directly convert a Time object to a DateTime object.

Below is the Ruby program to convert a unix timestamp (seconds since epoch) to Ruby DateTime using Time#to_datetime:

Ruby
# Ruby program to convert a unix timestamp 
# (seconds since epoch) to Ruby DateTime
require 'date'

def unix_timestamp_to_datetime(timestamp)
  # Convert Unix timestamp to a Time object
  time_object = Time.at(timestamp)
  
  # Convert Time object to DateTime object using Time#to_datetime
  datetime_object = time_object.to_datetime
  
  return datetime_object
end

# Example usage:
timestamp = 1625097600 
datetime = unix_timestamp_to_datetime(timestamp)
puts "DateTime: #{datetime}"
 

Output:

Using Time#to_datetime
Using Time#to_datetime

Conclusion

In this article we understood how to convert epoch time to a Ruby DateTime object using arious methods provided by the Time and DateTime classes or through DateTime.strptime. Each method offers simplicity and flexibility, allowing developers to choose the one that best suits their needs and preferences.


Article Tags :

Similar Reads