English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Date & Time (Date & Time)

Time The class in Ruby 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 197Or 0 years before 2038 The date after the year.

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:

Online Examples

#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-
 
time1 = Time.new
 
puts "Current time : " + time1.inspect
 
# Same as Time.now function
time2 = Time.now
puts "Current time : " + time2.inspect

The output result of the above example is as follows:

Current time : 2015-09-17 15:23:14 +0800
Current time : 2015-09-17 15:23:14 +0800

to get Date & Time components

We can use Time Objects to get various date and time components. See the following examples:

Online Examples

#!/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            # => a week of the week (0 is Sunday)
puts time.yday            # => 365: the day of the year
puts time.hour            # => 23:24 Hour system
puts time.min              # => 59
puts time.sec              # => 59
puts time.usec            # => 999999: microseconds
puts time.zone            # => "UTC": the name of the time zone

The output result of the above example is as follows:

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 Function

These functions can be used to format standard date formats, 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 (the same as UTC)
Time.gm(2008, 7, 8, 9, 10, 11)

# The following example retrieves all components in the array:

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

Try the following example:

Online Examples

#!/usr/bin/ruby -w
 
time = Time.new
 
values = time.to_a
p values

The output result of the above example is as follows:

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

The array can be passed to Time.utc or Time.local Functions to get different date formats are as follows:

Online Examples

#!/usr/bin/ruby -w
 
time = Time.new
 
values = time.to_a
puts Time.utc(*values)

The output result of the above example is as follows:

2015-09-17 15:26:09 UTC

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

# Returns the number of seconds since the epoch
time = Time.now.to_i  
 
# Converts the number of seconds to a Time object
Time.at(time)
 
# Returns the number of seconds since the epoch, including microseconds
time = Time.now.to_f

time zones and daylight saving time

to get all the information about time zones and daylight saving time, as shown below: Time You can use

time = Time.new
 
# Here is the explanation
time.zone       # => "UTC": Returns the time zone
time.utc_offset # => 0: UTC is relative to UTC with a 0 second offset
time.zone       # => "PST" (or other time zone)
time.isdst      # => false: If UTC does not have DST (Daylight Saving Time)
time.utc?       # => true: If in UTC time zone
time.localtime  # Converts to local time zone
time.gmtime     # Converts back to UTC
time.getlocal   # Returns a new Time object in the local time zone
time.getutc     # Returns a new Time object in UTC

Formatting Date and Time

There are many ways to format dates and times. The following examples demonstrate a part of them:

Online Examples

#!/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 as follows:

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 commands

The following table lists the commands and methods Time.strftime Used together.

CommandDescription
%aAbbreviated name of the day of the week (e.g., Sun).
%AFull name of the day of the week (e.g., Sunday).
%bAbbreviated name of the month (e.g., Jan).
%BFull name of the month (e.g., January).
%cPreferred local date and time representation.
%dDay number in a month (01 to 31)
%HHour number in a day,24 Timekeeping system (00 to 23)
%IHour number in a day,12 Timekeeping system (01 to 12)
%jDay number in the year (001 to 366)
%mMonth number in the year (01 to 12)
%MMinute in the hour (00 to 59)
%pMeridian indicator (AM or PM).
%SSecond in the minute (00 or 60).
%UWeek number in the current year, starting from the first Sunday (as the first day of the first week) (00 to 53)
%WWeek number in the current year, starting from the first Monday (as the first day of the first week) (00 to 53)
%wWhich day of the week in a week (Sunday is 0, 0 to 6)
%xPreferred representation with only date, without time.
%XPreferred representation with only time, without date.
%yYear without century representation (00 to 99)
%YYear with century.
%ZTime zone name.
%%% character.

Time Arithmetic

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

now = Time.now             # Current time
puts now
 
past = now - 10          # 10 seconds before. Time - number => Time
puts past
 
future = now + 10        # Start from now 10 seconds later. Time + number => Time
puts future
 
diff = future - now            # => 10  Time - Time => seconds
puts diff

The output result of the above example is as follows:

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