Ruby Date Time
# Ruby Date & Time
The **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 dates and times.
## Creating the Current Date and Time
Here is a simple example to get the current date and time:
## Example
```ruby
#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-
time1 = Time.new
puts "Current Time : " + time1.inspect
# Time.now has the same effect
time2 = Time.now
puts "Current Time : " + time2.inspect
[Try it yourself Β»](#)
The output of the above example will be:
Current Time : 2015-09-17 15:23:14 +0800
Current Time : 2015-09-17 15:23:14 +0800
## Getting Date & Time Components
We can use the _Time_ object to get various components of the date and time. See the example below:
## Example
```ruby
#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-
time = Time.new
# Components of a Time
puts "Current Time : " + time.inspect
puts time.year # => Date of the year
puts time.month # => Month of the year (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": Time zone name
The output of the above example will be:
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_ Functions
These functions can be used to format dates in standard format as shown below:
```ruby
# 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 components in an array:
[sec,min,hour,day,month,year,wday,yday,isdst,zone]
Try the following example:
## Example
```ruby
#!/usr/bin/ruby -w
time = Time.new
values = time.to_a
p values
The output of the above example will be:
[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"]
This array can be passed to _Time.utc_ or _Time.local_ functions to get dates in different formats, as shown below:
## Example
```ruby
#!/usr/bin/ruby -w
time = Time.new
values = time.to_a
puts Time.utc(*values)
The output of the above example will be:
2015-09-17 15:26:09 UTC
Here is another way to get time, in seconds since the epoch (platform-dependent):
```ruby
# Returns the number of seconds since the epoch
time = Time.now.to_i
# Convert seconds back 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 the _Time_ object to get all information related to time zones and daylight saving time, as shown below:
```ruby
time = Time.new
# Here is the explanation
time.zone # => "UTC": returns the time zone
time.utc_offset # => 0: UTC is 0 seconds offset from UTC
time.zone # => "PST" (or other time zone)
time.isdst # => false: If UTC does not have DST (daylight saving time)
time.utc? # => true: If in the UTC time zone
time.localtime # Convert to the local time zone
time.gmtime # Convert 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 Time and Date
There are multiple ways to format dates and times. The following example demonstrates some of them:
## Example
```ruby
#!/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 will be:
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 are used with the method _Time.strftime_.
| Directive | Description |
| --- | --- |
| %a | Abbreviated weekday name (e.g., Sun). |
| %A | Full weekday name (e.g., Sunday). |
| %b | Abbreviated month name (e.g., Jan). |
| %B | Full month name (e.g., 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 for the date alone, no time. |
| %X | Preferred representation for the time alone, no date. |
| %y | Year without a century (00 to 99). |
| %Y | Year with century. |
| %Z | Time zone name. |
| %% | Literal % character. |
## Time Arithmetic
You can do simple arithmetic with time, as shown below:
```ruby
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 output of the above example will be:
2015-09-17 15:27:08 +0800
2015-09-17 15:26:58 +0800
2015-09-17 15:27:18 +0800
10.0
YouTip