Latest web development tutorials

Ruby Date & Time (Date & Time)

Time class in Ruby for representing the date and time.It is based on the operating system provides the system date and time. This class may not indicate the date before 1970 or after 2038.

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

Creating the current date and time

Here is a simple example to obtain the current date and time:

Examples

#! / Usr / bin / ruby ​​-w
# - * - Coding: UTF-8 - * -

time1 = Time.new

puts "Current time:" + time1.inspect

# Time.now same function
time2 = Time.now
puts "Current time:" + time2.inspect

Running instance »

Run the above example output is:

当前时间 : 2015-09-17 15:23:14 +0800
当前时间 : 2015-09-17 15:23:14 +0800

Gets Date & Time Components

We can use theTimeobject to get the date and time of the various components. Consider the following examples:

#!/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":时区名称

Run the above example output is:

当前时间 : 2015-09-17 15:24:44 +0800
2015
9
17
4
260
15
24
44
921519
CST

Time.utc,Time.gm andTime.localfunction

These functions can be used to format the standard date format, 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 examples get all the components in the array:

[sec,min,hour,day,month,year,wday,yday,isdst,zone]

Try the following examples:

#!/usr/bin/ruby -w

time = Time.new

values = time.to_a
p values

Run the above example output is:

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

The array can be passedTime.utcorTime.localfunction to obtain a different date format, as follows:

#!/usr/bin/ruby -w

time = Time.new

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

Run the above example output is:

2015-09-17 15:26:09 UTC

Here 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 theTimeobject to get all the information and the time zone and daylight saving time related 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 对象

Time and date format

There are several ways to format date and time. The following example demonstrates part:

#!/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")

Run the above example output 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 method of instruction used in conjunction withTime.strftimelisted in the following table.

指令描述
%a星期几名称的缩写(比如 Sun)。
%A星期几名称的全称(比如 Sunday)。
%b月份名称的缩写(比如 Jan)。
%B月份名称的全称(比如 January)。
%c优选的本地日期和时间表示法。
%d一个月中的第几天(01 到 31)。
%H一天中的第几小时,24 小时制(00 到 23)。
%I一天中的第几小时,12 小时制(01 到 12)。
%j一年中的第几天(001 到 366)。
%m一年中的第几月(01 到 12)。
%M小时中的第几分钟(00 到 59)。
%p子午线指示(AM 或 PM)。
%S分钟中的第几秒(00 或 60)。
%U当前年中的周数,从第一个星期日(作为第一周的第一天)开始(00 到 53)。
%W当前年中的周数,从第一个星期一(作为第一周的第一天)开始(00 到 53)。
%w一星期中的第几天(Sunday 是 0,0 到 6)。
%x只有日期没有时间的优先表示法。
%X只有时间没有日期的优先表示法。
%y不带世纪的年份表示(00 到 99)。
%Y带有世纪的年份。
%Z时区名称。
%%% 字符。

Time Algorithm

You can use the time to do some simple arithmetic, as follows:

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

Run the above example output is:

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