Ruby on Rails Time, Date, DateTime Cheatsheet
Rails has a lot of handy methods built-in that sometimes all you have to do is know it. In consequence, there are an overwhelming methods available to get specific time and/or date.
This cheatsheet should provide some practical methods for Time, Date, DateTime that may help you in developing with Rails. I have also included an example output of each method so you don't have to manually enter it in rails console to know what the outcome would be.
Here is a brief overview of what methods will be covered in this cheatsheet. Feel free to skip to the methods that you need or scroll around to know what methods there are. Maybe it will help you in the future. Enjoy!
-
Current time and date (
now
,current
,today
) - Specific day (
yesterday
,tomorrow
) -
Specific moment in time (
beginning_of_minute
,end_of_minute
,beginning_of_hour
,end_of_hour
) -
Specific time of the day (
beginning_of_day
,middle_of_day
,end_of_day
) -
Specific time of a week (
beginning_of_week
,end_of_week
,last_week
,next_week
,last_weekday
,next_weekday
) -
Specific time of a month (
beginning_of_month
,end_of_month
,last_month
,next_month
) -
Specific time of a quarter (
beginning_of_quarter
,end_of_quarter
,last_quarter
,next_quarter
) -
Specific time of a year (
beginning_of_year
,end_of_year
,last_year
,next_year
)
Current time and date
Each classes have its own methods to get the current time or date.
Time
For Time
, it has now
. It returns current date and
time. Do notice that it automatically gives it in local time (with local
timezone offset).
Time.now // 2021-04-17 20:19:39.22369 +0700
Date
For Date
, it has current
and today
. It
returns date with the month and day's shorten name.
Date.current // Sat, 17 Apr 2021
Date.today // Sat, 17 Apr 2021
DateTime
DateTime
has both now
and
current
method. Both returns Date
's date style and
Time
's time style. The difference is now
gives the
local time and current
gives time in UTC.
DateTime.now // Sat, 17 Apr 2021 20:24:54 +0700
DateTime.current // Sat, 17 Apr 2021 13:24:58 +0000
All the following methods can be called with any Time, Date, and DateTime's instance.
Let's say you fetched a date/time/datetime from a database. You can find specific day relative to that time.
For each example, I have provided the relative time/date/datetime of when
the Time.now
, Date.current
,
DateTime.current
, and DateTime.now
was invoked.
Specific day
Let's say you want to get yesterday's or tomorrow's date.
Date
and DateTime
has the method of
yesterday
and tomorrow
.
// Current's date: Sat, 17 Apr 2021
Date.yesterday // Fri, 16 Apr 2021
DateTime.yesterday // Fri, 16 Apr 2021
Date.tomorrow // Sun, 18 Apr 2021
DateTime.tomorrow // Sun, 18 Apr 2021
Specific moment in time
For specific moment in a defined time, Time
and
DateTime
has several methods.
beginning_of_minute
beginning_of_minute
returns a time's start of minute.
// Current time: Sat, 17 Apr 2021 21:11:30 +0700
Time.now.beginning_of_minute
// 2021-04-17 21:11:00 +0700
DateTime.now.beginning_of_minute
// Sat, 17 Apr 2021 21:11:00 +0700
DateTime.current.beginning_of_minute
// Sat, 17 Apr 2021 14:11:00 +0000
end_of_minute
end_of_minute
returns a time's end of minute.
// Current time: Sat, 17 Apr 2021 21:11:30 +0700
Time.now.end_of_minute
// 2021-04-17 21:11:59.999999999 +0700
DateTime.now.end_of_minute
// Sat, 17 Apr 2021 21:11:59 +0700
DateTime.current.end_of_minute
// Sat, 17 Apr 2021 14:11:59 +0000
beginning_of_hour
beginning_of_hour
returns a time's start of hour.
// Current time: Sat, 17 Apr 2021 21:11:30 +0700
Time.now.beginning_of_hour
// 2021-04-17 21:00:00 +0700
DateTime.now.beginning_of_hour
// Sat, 17 Apr 2021 21:00:00 +0700
DateTime.current.beginning_of_hour
// Sat, 17 Apr 2021 14:00:00 +0000
end_of_hour
end_of_hour
returns a time's end of hour.
// Current time: Sat, 17 Apr 2021 21:11:30 +0700
Time.now.end_of_hour
// 2021-04-17 21:59:59.999999999 +0700
DateTime.now.end_of_hour
// Sat, 17 Apr 2021 21:59:59 +0700
DateTime.current.end_of_hour
// Sat, 17 Apr 2021 14:59:59 +0000
Specific time of the day
All classes have beginning_of_day
, middle_of_day
,
and end_of_day
to instantly get a specific time of a defined date
and time.
beginning_of_day
beginning_of_day
gives time at 12 pm (midnight).
It is also aliased as:
midnight
at_midnight
at_beginning_of_day
// Current's date: Sat, 17 Apr 2021
Time.now.beginning_of_day
// 2021-04-17 00:00:00 +0700
Date.current.beginning_of_day
// Sat, 17 Apr 2021 00:00:00 UTC +00:00
DateTime.now.beginning_of_day
// Sat, 17 Apr 2021 00:00:00 +0700
DateTime.current.beginning_of_day
Sat, 17 Apr 2021 00:00:00 +0000
middle_of_day
middle_of_day
gives time at 12 pm (noon/midday).
It also aliased as
midday
noon
at_midday
at_noon
at_middle_of_day
// Current's date: Sat, 17 Apr 2021
Time.now.middle_of_day
// 2021-04-17 12:00:00 +0700
Date.current.middle_of_day
// Sat, 17 Apr 2021 12:00:00 UTC +00:00
DateTime.now.middle_of_day
// Sat, 17 Apr 2021 12:00:00 +0700
DateTime.current.middle_of_day
// Sat, 17 Apr 2021 12:00:00 +0000
end_of_day
end_of_day
returns time at 11.59 pm. It is also
aliased as at_end_of_day
.
// Current's date: Sat, 17 Apr 2021
Time.now.end_of_day
// 2021-04-17 23:59:59.999999999 +0700
Date.current.end_of_day
// Sat, 17 Apr 2021 23:59:59 UTC +00:00
DateTime.now.end_of_day
// Sat, 17 Apr 2021 23:59:59 +0700
DateTime.current.end_of_day
// Sat, 17 Apr 2021 23:59:59 +0000
Specific day in a week
Rails have interesting methods for get a specific day based on a week. One thing to note that a week starts on Monday and ends on Sunday.
beginning_of_week
beginning_of_week
returns the date of the first day of the week
(Monday) and beginning of day in a time's week.
// Current date: Sat, 17 Apr 2021
Time.now.beginning_of_week
// 2021-04-12 00:00:00 +0700
Date.current.beginning_of_week
// Mon, 12 Apr 2021
DateTime.current.beginning_of_week
// Mon, 12 Apr 2021 00:00:00 +0000
DateTime.now.beginning_of_week
// Mon, 12 Apr 2021 00:00:00 +0700
end_of_week
end_of_week
returns date of the last day of the week (Sunday) and
end of day in a time's week. It is also aliased as
at_end_of_week
.
// Current date: Sat, 17 Apr 2021
Time.now.end_of_week
// 2021-04-18 23:59:59.999999999 +0700
Date.current.end_of_week
// Sun, 18 Apr 2021
DateTime.current.end_of_week
// Sun, 18 Apr 2021 23:59:59 +0000
DateTime.now.end_of_week
// Sun, 18 Apr 2021 23:59:59 +0700
last_week
last_week
returns the beginning of the previous week. It is also
aliased as prev_week
.
// Current date: Sat, 17 Apr 2021
Time.now.last_week
// 2021-04-05 00:00:00 +0700
Date.current.last_week
// Mon, 05 Apr 2021
DateTime.current.last_week
// Mon, 05 Apr 2021 00:00:00 +0000
DateTime.now.last_week
// Mon, 05 Apr 2021 00:00:00 +0700
next_week
next_week
returns the beginning of the following week.
// Current date: Sat, 17 Apr 2021
Time.now.next_week
// 2021-04-19 00:00:00 +0700
Date.current.next_week
// Mon, 19 Apr 2021
DateTime.current.next_week
// Mon, 19 Apr 2021 00:00:00 +0000
DateTime.now.next_week
// Mon, 19 Apr 2021 00:00:00 +0700
last_weekday
last_weekday
returns the last day of weekday and beginning of
time from the defined time. It is also aliased as prev_weekday
.
On a weekend's date (Saturday and Sunday), last_weekday
returns
Friday of the current week.
// Current time: Sun, 18 Apr 2021 07:48:44 +0000
Time.now.last_weekday
// 2021-04-16 14:48:44.26745 +0700
Date.current.last_weekday
// Fri, 16 Apr 2021
DateTime.current.last_weekday
// Fri, 16 Apr 2021 07:48:44 +0000
DateTime.now.last_weekday
// Fri, 16 Apr 2021 14:48:44 +0700
On a weekday, last_weekday
returns yesterday or Friday of the
previous week if it was a Monday's date.
// Current time: Fri, 16 Apr 2021 07:48:44 +0000
Time.now.last_weekday
// 2021-04-16 14:48:44.981865 +0700
Date.current.last_weekday
// Thu, 15 Apr 2021
DateTime.current.last_weekday
// Thu, 15 Apr 2021 07:48:44 +0000
DateTime.now.last_weekday
// Thu, 15 Apr 2021 14:48:44 +0700
// Current time: Mon, 12 Apr 2021 07:48:44 +0000
Time.now.last_weekday
// 2021-04-16 14:48:44.708858 +0700
Date.current.last_weekday
// Fri, 09 Apr 2021
DateTime.current.last_weekday
// Fri, 09 Apr 2021 07:48:44 +0000
DateTime.now.last_weekday
// Fri, 09 Apr 2021 14:48:44 +0700
next_weekday
next_weekday
returns the next weekday of the defined time. It
generally follows the same rules as last_weekday
// Current time: Sun, 18 Apr 2021 07:48:44 +0000
Time.now.next_weekday
// 2021-04-19 14:48:44.758617 +0700
Date.current.next_weekday
// Mon, 19 Apr 2021
DateTime.current.next_weekday
// Mon, 19 Apr 2021 07:48:44 +0000
DateTime.now.next_weekday
// Mon, 19 Apr 2021 14:48:44 +0700
// Current time: Mon, 12 Apr 2021 07:48:44 +0000
Time.now.next_weekday
// 2021-04-12 14:48:44.24809 +0700
Date.current.next_weekday
// Tue, 13 Apr 2021
DateTime.current.next_weekday
// Tue, 13 Apr 2021 07:48:44 +0000
DateTime.now.next_weekday
// Tue, 13 Apr 2021 14:48:44 +0700
// Current time: Fri, 16 Apr 2021 07:48:44 +0000
Time.now.next_weekday
// 2021-04-19 14:48:44.465501 +0700
Date.current.next_weekday
// Mon, 19 Apr 2021
DateTime.current.next_weekday
// Mon, 19 Apr 2021 07:48:44 +0000
DateTime.now.next_weekday
// Mon, 19 Apr 2021 14:48:44 +0700
Specific time in a month
beginning_of_month
beginning_of_month
returns the first day of the month dan its
beginning of day. It is also aliased as at_beginning_of_month
// Current date: Sun, 18 Apr 2021
Time.now.beginning_of_month
// 2021-04-01 00:00:00 +0700
Date.current.beginning_of_month
// Thu, 01 Apr 2021
DateTime.current.beginning_of_month
// Thu, 01 Apr 2021 00:00:00 +0000
DateTime.now.beginning_of_month
// Thu, 01 Apr 2021 00:00:00 +0700
end_of_month
end_of_month
returns the last date of the month and its end of
day time. It is also aliased as at_end_of_month
.
// Current date: Sun, 18 Apr 2021
Time.now.end_of_month
// 2021-04-30 23:59:59.999999999 +0700
Date.current.end_of_month
// Fri, 30 Apr 2021
DateTime.current.end_of_month
// Fri, 30 Apr 2021 23:59:59 +0000
DateTime.now.end_of_month
// Fri, 30 Apr 2021 23:59:59 +0700
last_month
Date & DateTime #last_month
(alias prev_month
)
// Current time: Sun, 18 Apr 2021 08:29:19 +0000
Time.now.last_month
// 2021-03-18 15:29:19.529931 +0700
Date.current.last_month
// Thu, 18 Mar 2021
DateTime.current.last_month
// Thu, 18 Mar 2021 08:29:19 +0000
DateTime.now.last_month
// Thu, 18 Mar 2021 15:29:19 +0700
next_month
Date & DateTime #next_month
// Current time: Sun, 18 Apr 2021 08:29:19 +0000
Time.now.next_month
// 2021-05-18 15:29:19.529931 +0700
Date.current.next_month
// Tue, 18 May 2021
DateTime.current.next_month
// Tue, 18 May 2021 08:29:19 +0000
DateTime.now.next_month
// Tue, 18 May 2021 15:29:19 +0700
Specific time in a quarter
Sometimes we need quarterly period of a certain time. Rails has the usual beginning-end and last-next for quarters too.
beginning_of_quarter
beginning_of_quarter
returns the first day of the quarter's month
and its beginning of day's time.
// Current date: Sun, 18 Apr 2021
Time.now.beginning_of_quarter
// 2021-04-01 00:00:00 +0700
Date.current.beginning_of_quarter
// Thu, 01 Apr 2021
DateTime.current.beginning_of_quarter
// Thu, 01 Apr 2021 00:00:00 +0000
DateTime.now.beginning_of_quarter
// Thu, 01 Apr 2021 00:00:00 +0700
end_of_quarter
end_of_quarter
returns the last day of the quarter's month and
its end of day's time.
// Current date: Sun, 18 Apr 2021
Time.now.end_of_quarter
// 2021-06-30 23:59:59.999999999 +0700
Date.current.end_of_quarter
// Wed, 30 Jun 2021
DateTime.current.end_of_quarter
// Wed, 30 Jun 2021 23:59:59 +0000
DateTime.now.end_of_quarter
// Wed, 30 Jun 2021 23:59:59 +0700
prev_quarter
prev_quarter
returns previous quarter's date and time relative to
a defined time.
// Current time: Sun, 18 Apr 2021 13:10:35 +0000
Time.now.prev_quarter
// 2021-01-18 20:10:35.383998 +0700
Date.current.prev_quarter
// Mon, 18 Jan 2021
DateTime.current.prev_quarter
// Mon, 18 Jan 2021 13:10:35 +0000
DateTime.now.prev_quarter
// Mon, 18 Jan 2021 20:10:35 +0700
next_quarter
next_quarter
returns the next quarter's date and time relative to
a defined time.
// Current time: Sun, 18 Apr 2021 13:10:35 +0000
Time.now.next_quarter
// 2021-07-18 20:10:35.383998 +0700
Date.current.next_quarter
// Sun, 18 Jul 2021
DateTime.current.next_quarter
// Sun, 18 Jul 2021 13:10:35 +0000
DateTime.now.next_quarter
// Sun, 18 Jul 2021 20:10:35 +0700
Specific time in a year
Similar to monthly or quarterly date and time, there is also for yearly duration.
beginning_of_year
beginning_of_year
returns the year's first date and beginning of
day's time.
// Current date: Sun, 18 Apr 2021
Time.now.beginning_of_year
// 2021-01-01 00:00:00 +0700
Date.current.beginning_of_year
// Fri, 01 Jan 2021
DateTime.current.beginning_of_year
// Fri, 01 Jan 2021 00:00:00 +0000
DateTime.now.beginning_of_year
// Fri, 01 Jan 2021 00:00:00 +0700
end_of_year
end_of_year
returns the year's last date and end of day's time.
// Current date: Sun, 18 Apr 2021
Time.now.end_of_year
// 2021-12-31 23:59:59.999999999 +0700
Date.current.end_of_year
// Fri, 31 Dec 2021
DateTime.current.end_of_year
// Fri, 31 Dec 2021 23:59:59 +0000
DateTime.now.end_of_year
// Fri, 31 Dec 2021 23:59:59 +0700
last_year
last_year
returns the date of last year relative to a defined
time.
// Current time: Sun, 18 Apr 2021 13:20:58 +0000
Time.now.last_year
// 2020-04-18 20:20:58.898767 +0700
Date.current.last_year
// Sat, 18 Apr 2020
DateTime.current.last_year
// Sat, 18 Apr 2020 13:20:58 +0000
DateTime.now.last_year
// Sat, 18 Apr 2020 20:20:58 +0700
next_year
next_year
returns the date of the following year relative to a
defined time.
// Current time: Sun, 18 Apr 2021 13:20:58 +0000
Time.now.next_year
// 2021-04-18 20:20:58.898767 +0700
Date.current.next_year
// Mon, 18 Apr 2022
DateTime.current.next_year
// Mon, 18 Apr 2022 13:20:58 +0000
DateTime.now.next_year
// Mon, 18 Apr 2022 20:20:58 +0700
If you want to know all methods that is available or not available to each classes, I have compiled a table of methods from Rails Date, Time, and DateTime's documentation. You can also see aliases for the methods. It's not perfect but it is somewhere to start rather than manually comparing it in Rails's (sometimes incomplete) documentation.
Comments
Post a Comment