Epoch and Unix Timestamp Conversion in Ruby
Ruby is a popular interpreted, high-level, general-purpose programming language. Ruby is a dynamically typed, uses garbage collection and supports multiple programming paradigms, including procedural, object-oriented, and functional programming. It has class DateTime to handle epoch or Unix timestamp conversion into human readable dates or can convert human readable dates to Unix timestamp.
Here we will explain Ruby date time functions to get current epoch or Unix timestamp, convert timestamp to date and convert date to epoch or Unix timestamp.
Get current epoch or Unix timestamp in RubyWe will use the DateTime
to get the current timestamp.
currentTimestamp = DateTime.now
Convert epoch or Unix timestamp to date in Ruby
We will use the DateTime.strptime
to convert the epoch or timestamp to readable date format.
DateTime.strptime("1745572751",'%s')
Convert date to epoch or unix timestamp in Ruby
We can convert human readable date to timestamp using ToUnixTimeMilliseconds()
method.
DateTime.new(2025, 2, 21).to_time.to_i
More about date time in Ruby
Also, read:
- Epoch and Unix Timestamp Conversion in PHP
- Epoch and Unix Timestamp Conversion in Java
- Epoch and Unix Timestamp Conversion in JavaScript
- Epoch and Unix Timestamp Conversion in Perl
- Epoch and Unix Timestamp Conversion in Python
- Epoch and Unix Timestamp Conversion in TypeScript
- Epoch and Unix Timestamp Conversion in MySQL
- Epoch and Unix Timestamp Conversion SQL Server
- Epoch and Unix Timestamp Conversion in C# (C-Sharp)
- Epoch and Unix Timestamp Conversion in Kotlin
- Epoch and Unix Timestamp Conversion in Go
- Epoch and Unix Timestamp Conversion in VBA
- Epoch and Unix Timestamp Conversion in MATLAB
- Epoch and Unix Timestamp Conversion in Rust