Ruby Date & Time


Time class is used to represent dates and times in Ruby. It is based on the system date and time provided by the operating system. This class may not represent dates before 1970 or after 2038.

This tutorial will familiarize you with all important concepts of date and time.

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 "当前时间 : " + time1.inspect

# Time.now 功能相同
time2 = Time.now
puts "当前时间 : " + time2.inspect

Run Instance»

Click the "Run Instance" button to view the online instance

The output result of the above example is:

当前时间 : 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. Please look at the following example:

#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-

time = Time.new

# Time 的组件
puts "当前时间 : " + time.inspect
puts time.year    # => 日期的年份
puts time.month   # => 日期的月份(1 到 12)
puts time.day     # => 一个月中的第几天(1 到 31)
puts time.wday    # => 一周中的星期几(0 是星期日)
puts time.yday    # => 365:一年中的第几天
puts time.hour    # => 23:24 小时制
puts time.min     # => 59
puts time.sec     # => 59
puts time.usec    # => 999999:微秒
puts time.zone    # => "UTC":时区名称

The output results of the above example are:

当前时间 : 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 formats, as follows:

# July 8, 2008
Time.local(2008, 7, 8)  
# July 8, 2008, 09:10am,本地时间
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 (与 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:

#!/usr/bin/ruby -w

time = Time.new

values = time.to_a
p values

The output result of the above example is:

[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"]

This array can be passed to Time.utc or Time. local function to get the different formats of the date, as follows:

#!/usr/bin/ruby -w

time = Time.new

values = time.to_a
puts Time.utc(*values)

The output result of the above example is:

2015-09-17 15:26:09 UTC

The following is the way to get the time, the number of seconds since the epoch (Platform dependent):

# 返回从纪元以来的秒数
time = Time.now.to_i  

# 把秒数转换为 Time 对象
Time.at(time)

# 返回从纪元以来的秒数,包含微妙
time = Time.now.to_f

Time Zone and Daylight Saving Time

You can use the Time object to get all information related to time zone and daylight saving time as follows:

time = Time.new

# 这里是解释
time.zone       # => "UTC":返回时区
time.utc_offset # => 0:UTC 是相对于 UTC 的 0 秒偏移
time.zone       # => "PST"(或其他时区)
time.isdst      # => false:如果 UTC 没有 DST(夏令时)
time.utc?       # => true:如果在 UTC 时区
time.localtime  # 转换为本地时区
time.gmtime     # 转换回 UTC
time.getlocal   # 返回本地区中的一个新的 Time 对象
time.getutc     # 返回 UTC 中的一个新的 Time 对象

Formatting Time and Date

There are many ways to format dates and times. The following example demonstrates part of it:

#!/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 output result of the above example is:

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 instructions

The instructions and methods listed in the following table## Used together with #Time.strftime.

CommandDescription
%aThe abbreviation of the name of the day of the week (such as Sun).
%AThe full name of the day of the week (such as Sunday).
%bThe abbreviation of the month name (such as Jan).
%BThe full name of the month (such as January).
%cPreferred local date and time representation.
%dThe day of the month (01 to 31).
%HThe hour of the day, 24-hour clock (00 to 23).
%IThe hour of the day, 12-hour clock (01 to 12).
%jThe day of the year (001 to 366).
%mThe month of the year (01 to 12).
%MThe minute number of the hour (00 to 59).
%p Meridian indication (AM or PM).
%SThe second in minutes (00 or 60).
%UThe week number in the current year, starting with the first Sunday (as the first day of the first week) (00 to 53).
%WThe week number in the current year, starting with the first Monday (as the first day of the first week) (00 to 53).
%wThe day of the week (Sunday is 0, 0 to 6).
%xPreferred representation with only date and no time.
%XPreferred representation with only time and no date.
%y Year representation without century (00 to 99).
%YYear with century.
%ZTime zone name.
%%% characters.

Time Algorithm

You can do some simple arithmetic with time, as shown below:

now = Time.now           # 当前时间
puts now

past = now - 10          # 10 秒之前。Time - number => Time
puts past

future = now + 10        # 从现在开始 10 秒之后。Time + number => Time
puts future

diff = future - now      # => 10  Time - Time => 秒数
puts diff

The above example runs The output result is:

2015-09-17 15:27:08 +0800
2015-09-17 15:26:58 +0800
2015-09-17 15:27:18 +0800
10.0