Ruby Date & Time

TimeThe 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 date and time.

Creating the current date and time

The following 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 equivalent time2 = Time.now puts "Current time :" + time2.inspect

Try it »

The output of the above example is:

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

Getting Date & Time Components

We can useTimeTime objects 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 #=> 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 #=> 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": Timezone name

The output of the above example is:

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

Time.utc、Time.gmandTime.localFunctions

These functions can be used to format dates in standard 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 (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 output of the above example is:

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

This array can be passed to theTime.utcorTime.localfunction 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 output of the above example is:

2015-09-17 15:26:09 UTC

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

#Returns the number of seconds since the epoch time = Time.now.to_i #Convert 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

You can use TimeTimeobjects 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 timezone time.utc_offset #=> 0: UTC is 0 seconds offset relative to UTC time.zone #=> "PST" (or other timezone) time.isdst #=> false: if UTC has no DST (daylight saving time) time.utc? #=> true: if in UTC timezone time.localtime #Convert to local timezone time.gmtime #Convert back to UTC time.getlocal #Returns a new Time object in the local zone time.getutc #Returns a new Time object in UTC

Formatting Time and Date

There are many ways to format date and time. 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 output 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 Directives

The directives listed in the table below are used with theTime.strftimemethod.

DirectiveDescription
%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 of the month (01 to 31).
%HHour of the day, 24-hour clock (00 to 23).
%IHour of the day, 12-hour clock (01 to 12).
%jDay of the year (001 to 366).
%mMonth of the year (01 to 12).
%MMinute of the hour (00 to 59).
%pMeridian indicator (AM or PM).
%SSecond of the minute (00 or 60).
%UWeek number of the current year, starting from the first Sunday (as the first day of the first week) (00 to 53).
%WWeek number of the current year, starting from the first Monday (as the first day of the first week) (00 to 53).
%wDay of the week (Sunday is 0, 0 to 6).
%xPreferred representation for date only, with no time.
%XPreferred representation for time only, with no date.
%yYear representation without century (00 to 99).
%YYear with century.
%ZTimezone name.
%%% 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 => seconds puts diff

The output of the above example is:

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