Ruby Date & Time
Time The class is used in Ruby 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.
Create the Current Date and Time
The following is a simple example of getting the current date and time:
Example
!/usr/bin/ruby -w
-- coding: UTF-8 --
time1
=
Time
.
new
puts
"
Current time :
"
time1
.
inspect
Time.now does the same thing
time2
=
Time
.
now
puts
"
Current time :
"
time2
.
inspect
Try it »
The above example produces the following output:
当前时间 : 2015-09-17 15:23:14 +0800
当前时间 : 2015-09-17 15:23:14 +0800
Get 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 Time
puts
"
Current time :
"
time
.
inspect
puts
time
.
year
=> the year of the date
puts
time
.
month
=> the month of the date (1 to 12)
puts
time
.
day
=> the day of the month (1 to 31)
puts
time
.
wday
=> the day of the week (0 is Sunday)
puts
time
.
yday
=> 365: the 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 produces the following output:
当前时间 : 2015-09-17 15:24:44 +0800
2015
9
17
4
260
15
24
44
921519
CST
Time.utc、Time.gm and Time.local function
These functions can be used to format dates in a 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 produces the following output:
[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"]
This array can be passed to the Time.utc or Time.local function to get different formats of the date, as shown below:
Example
!/usr/bin/ruby -w
time
=
Time
.
new
values
=
time
.
to_a
puts
Time
.
utc
(
values
)
The above example produces the following output:
2015-09-17 15:26:09 UTC
The following is a way to get time as the number of seconds since the epoch (platform dependent):
Return the number of seconds since the epoch
time
=
Time
.
now
.
to_i
Convert seconds to a Time object
Time
.
at
(
time
)
Return the number of seconds since the epoch, 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 explanation
time
.
zone
=> "UTC": returns the time zone
time
.
utc_offset
=> 0: UTC is a 0-second offset relative to UTC
time
.
zone
=> "PST" (or another time zone)
time
.
isdst
=> false: if UTC has no DST (daylight saving time)
time
.
utc
?
=> true: if in the UTC time zone
time
.
localtime
Convert to local time zone
time
.
gmtime
Convert back to UTC
time
.
getlocal
Return a new Time object in the local time zone
time
.
getutc
Return a new Time object in UTC
Formatting Time and Date
There are many 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 produces the following output:
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 directives listed in the following table and the methods Time.strftime .
| Instruction | Description |
|---|---|
| %a | Abbreviated weekday name (for example, Sun). |
| %A | Full weekday name (for example, Sunday). |
| %b | Abbreviated month name (for example, Jan). |
| %B | Full month name (for example, 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 with only the date and no time. |
| %X | Preferred representation with only the time and no date. |
| %y | Year without century (00 to 99). |
| %Y | Year with century. |
| %Z | Time zone name. |
| %% | The % character. |
Time Arithmetic
You can do some 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 produces the following output:
2015-09-17 15:27:08 +0800
2015-09-17 15:26:58 +0800
2015-09-17 15:27:18 +0800
10.0