Ruby Date & Time (Date & Time)
The Time class in Ruby is used to represent dates and times. It is based on the system date and time provided by the operating system. This class may not be able to represent dates before 1970 or after 2038.
This tutorial will familiarize you with all the important concepts of dates and times.
Creating the Current Date and Time
Below is a simple example to get the current date and time:
Example
#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-
time1 = Time.new
puts "Current Time : " + time1.inspect
# Time.now is an equivalent method
time2 = Time.now
puts "Current Time : " + time2.inspect
The above example outputs the following result:
Current Time : 2015-09-17 15:23:14 +0800
Current Time : 2015-09-17 15:23:14 +0800
Getting Date & Time Components
We can use the Time object to get various date and time components. See the following example:
Example
#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-
time = Time.new
# Components of a Time
puts "Current Time : " + time.inspect
puts time.year # => Year of the date
puts time.month # => Month of the date (1 to 12)
puts time.day # => Day of the month (1 to 31)
puts time.wday # => 0: Day of the week (0 is Sunday)
puts time.yday # => 365: Day of the year
puts time.hour # => 23: 24-hour clock
puts time.min # => 59
puts time.sec # => 59
puts time.usec # => 999999: Microseconds
puts time.zone # => "UTC": Time zone name
The above example outputs the following result:
Current Time : 2015-09-17 15:24:44 +0800
2015
9
17
4
260
15
24
44
921519
CST
Time.utc, Time.gm, and Time.local Functions
These functions can be used to format dates in standard format, as shown below:
# July 8, 2008
Time.local(2008, 7, 8)
# July 8, 2008, 09:10am, local time
Time.local(2008, 7, 8, 9, 10)
# July 8, 2008, 09:10 UTC
Time.utc(2008, 7, 8, 9, 10)
# July 8, 2008, 09:10:11 GMT (same as UTC)
Time.gm(2008, 7, 8, 9, 10, 11)
The following example gets all the components in an array:
[sec, min, hour, day, month, year, wday, yday, isdst, zone]
Try the following example:
Example
#!/usr/bin/ruby -w
time = Time.new
values = time.to_a
p values
The above example outputs the following result:
[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"]
This array can be passed to Time.utc or Time.local functions to get different formats of dates, as shown below:
Example
#!/usr/bin/ruby -w
time = Time.new
values = time.to_a
puts Time.utc(*values)
The above example outputs the following result:
2015-09-17 15:26:09 UTC
Here is how to get time, in seconds since the Epoch (platform-dependent):
# Returns seconds since Epoch
time = Time.now.to_i
# Convert number of seconds into Time object
Time.at(time)
# Returns current time, including microseconds
time = Time.now.to_f
Time Zones and Daylight Saving Time
You can use the Time object to get all information related to time zones and daylight saving time, as shown below:
time = Time.new
# Here is the interpretation
time.zone # => "UTC": returns the timezone
time.utc_offset # => 0: UTC is 0 seconds offset from UTC
time.zone # => "PST" (or whatever the timezone is)
time.isdst # => false: If UTC does not have DST (Daylight Saving Time)
time.utc? # => true: If in UTC time zone
time.localtime # Convert to local timezone
time.gmtime # Convert back to UTC
time.getlocal # Returns a new Time object in local zone
time.getutc # Returns a new Time object in UTC
Formatting Time and Date
There are various ways to format dates and times. The following example demonstrates some of them:
Example
#!/usr/bin/ruby -w
time = Time.new
puts time.to_s
puts time.ctime
puts time.localtime
puts time.strftime("%Y-%m-%d %H:%M:%S")
The above example outputs the following result:
2015-09-17 15:26:42 +0800
Thu Sep 17 15:26:42 2015
2015-09-17 15:26:42 +0800
2015-09-17 15:26:42
Time Formatting Directives
The following directives can be used with the Time.strftime method:
Directive | Description |
---|---|
%a | Abbreviated weekday name (e.g., Sun) |
%A | Full weekday name (e.g., Sunday) |
%b | Abbreviated month name (e.g., Jan) |
%B | Full month name (e.g., January) |
%c | 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 or 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
You can perform simple arithmetic with time, as shown below:
now = Time.now # Current time
puts now
past = now - 10 # 10 seconds ago. Time - number => Time
puts past
future = now + 10 # 10 seconds from now. Time + number => Time
puts future
diff = future - now # => 10 Time - Time => number of seconds
puts diff
The above example outputs the following result:
2015-09-17 15:27:08 +0800
2015-09-17 15:26:58 +0800
2015-09-17 15:27:18 +0800
10.0