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
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
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:
The following example gets all the components in an array:
Try the following example:
Example
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
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):
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:
Formatting Time and Date
There are many ways to format date and time. The following example demonstrates some of them:
Example
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.
| Directive | Description |
|---|---|
| %a | Abbreviated name of the day of the week (e.g., Sun). |
| %A | Full name of the day of the week (e.g., Sunday). |
| %b | Abbreviated name of the month (e.g., Jan). |
| %B | Full name of the month (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 from the first Sunday (as the first day of the first week) (00 to 53). |
| %W | Week number of the current year, starting from 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 date only, with no time. |
| %X | Preferred representation for time only, with no date. |
| %y | Year representation without century (00 to 99). |
| %Y | Year with century. |
| %Z | Timezone name. |
| %% | % character. |
Time Arithmetic
You can do some simple arithmetic with time, as shown below:
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.0Other Extensions