Without using any Ruby gems, Time is the class we can use when working with dates and times. It represents the number of seconds with fraction since the Epoch, January 1, 1970, 00:00 UTC. The Time class treats GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time) equivalent. GMT is the older way of referring to these baseline times but persists in the names of calls on POSIX systems.
Since Ruby 1.9.2, Time implementation uses a signed 63-bit integer, Bignum or Rational, to store the number of nanoseconds since the Epoch, representing 1823-11-12 to 2116-02-20. When Bignum or Rational is used (before 1823, after 2116, under nanosecond), Time works slower when an integer is used.
Creating Dates and Times
There are several ways to create dates and times in Rust.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # Create the current date/time current_date = Time.new puts current_date # Explicitly Create a date passing the year, the month and the day date_20200321 = Time.new(2020, 03, 21) puts date_20200321 # Explicitly create a date passing only the year first_date_of_2020 = Time::new(2020) puts first_date_of_2020 # Explicitly create a date passing only the year, and the month first_date_of_2020_december = Time::new(2020, 12) puts first_date_of_2020_december # Explicitly create a date passing only the year, the month, and the day first_date_of_2020_december_25 = Time::new(2020, 12, 25) puts first_date_of_2020_december_25 # Explicitly create a date passing only the year, the month, the day, the hour, the minute, and the second first_date_of_2020_december_25_14_03_12 = Time::new(2020, 12, 25, 14, 3, 12) puts first_date_of_2020_december_25_14_03_12 # Explicitly create a date with time and UTC offset first_date_of_2020_december_25_14_03_12_utc_offset = Time.new(2020, 3, 21, 1, 3, 3, "+07:00") puts first_date_of_2020_december_25_14_03_12_utc_offset |
The example produces the following output.
1 2 3 4 5 6 7 | 2020-03-21 13:12:42 +0800 2020-03-21 00:00:00 +0800 2020-01-01 00:00:00 +0800 2020-12-01 00:00:00 +0800 2020-12-25 00:00:00 +0800 2020-12-25 14:03:12 +0800 2020-03-21 01:03:03 +0700 |
Components Of Date and Time
The following is a working example to get the current date and time in Ruby. It also displays the date components – day, month, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 | current_date = Time.new puts "Current Time : " + current_date.inspect puts current_date.year #=> Year of the date puts current_date.month #=> Month of the date (1 to 12) puts current_date.day #=> Day of the date (1 to 31 ) puts current_date.wday # => 0: Day of week: 0 is Sunday puts current_date.yday #=> 365: Day of year puts current_date.hour #=> 23: 24-hour clock puts current_date.min #=> 59 puts current_date.sec #=> 59 puts current_date.usec #=> 999999: microseconds puts current_date.zone #=> "UTC": timezone name |
The example produces the following output.
1 2 3 4 5 6 7 8 9 10 11 | Current Time : 2020-03-21 12:16:48.4697248 +0800 2020 3 21 6 81 12 16 48 469724 Malay Peninsula Standard Time |
Timezones and Daylight Savings Time
We can also use the Time object to get the information associated with Timezones and daylight savings. It is helpful when working with zones, UTC offsets, etc.
1 2 3 4 5 6 7 8 9 10 11 | time = Time.new time.zone # => "UTC": return the timezone time.utc_offset # => 0: UTC is 0 seconds offset from UTC time.zone # => "PST" (or whatever your timezone is) time.isdst # => false: If UTC does not have DST. time.utc? # => true: if t is in UTC time zone time.localtime # Convert to local timezone. time.gmtime # Convert back to UTC. time.getlocal # Return a new Time object in local zone time.getutc # Return a new Time object in UTC |
Working With Formatting Times and Dates
There are various ways to work with dates and times in various formats. Here are a few.
1 2 3 4 5 | time = Time.new puts time.to_s puts time.ctime puts time.localtime puts time.strftime("%Y-%m-%d %H:%M:%S") |
The codes will output the following result.
1 2 3 4 | 2020-03-21 13:40:15 +0800 Sat Mar 21 13:40:15 2020 2020-03-21 13:40:15 +0800 2020-03-21 13:40:15 |
Formatting Directives
We can use the following directives with the method Time.strftime in Ruby.
Directive | Description |
---|---|
%a | The abbreviated weekday name (Sun). |
%A | The full weekday name (Sunday). |
%b | The abbreviated month name (Jan). |
%B | The full month name (January). |
%c | The preferred local date and time representation. |
%d | Day of the month (01 to 31). |
%H | Hour of the day, 24-hour clock (00 to 23). |
%I | Hour of the day, 12-hour clock (01 to 12). |
%j | Day of the year (001 to 366). |
%m | Month of the year (01 to 12). |
%M | Minute of the hour (00 to 59). |
%p | Meridian indicator (AM or PM). |
%S | Second of the minute (00 to 60). |
%U | Week number of the current year, starting with the first Sunday as the first day of the first week (00 to 53). |
%W | Week number of the current year, starting with the first Monday as the first day of the first week (00 to 53). |
%w | Day of the week (Sunday is 0, 0 to 6). |
%x | Preferred representation for the date alone, no time. |
%X | Preferred representation for the time alone, no date. |
%y | Year without a century (00 to 99). |
%Y | Year with century. |
%Z | Time zone name. |
%% | Literal % character. |
Time Arithmetic
We can perform date and time arithmetic in Ruby as follows. In line 6, we subtracted one hour from the current date and time. Then, we create a future date by adding 1.5 hours to the current date. Lastly, we computed the difference between two dates resulting in a number of seconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Current time current_datetime = Time.now puts current_datetime # Time - number of seconds => Time past = current_datetime - (60 * 60) # 1 Hour ago puts past # Time + number of seconds => Time future = current_datetime + (60 * 60) + 30 # 1 hour and 30 seconds from now puts future # Time - Time => number of seconds diff = future - past # => 7230.0 seconds puts diff |
The codes will produce the following result.
1 2 3 4 | 2020-03-21 13:32:20 +0800 2020-03-21 12:32:20 +0800 2020-03-21 14:32:50 +0800 7230.0 |