Skip to content

Working with Dates and Times

Last updated on June 30th, 2024 at 03:04 pm

Ruby has three objects that deal with dates and times – Date, Time and DateTime. From their names, it is easy to determine which object you should use in which circumstance. Date is used when you only care about dates and not times, Time is used when you only care about time and not date information. DateTime is to be used when you care about both date and time.

That said, one can feel a bit misled because the Time object has methods for date information, such as day, month and year. However, if you dig a little deeper you can see that the DateTime object has greater depth when it comes to date related information.

One quick note, however, is that the Date object is not included in the Ruby standard library, so you will have to use the command require 'date' wherever you want to use the Date object.

Let’s have a quick look at the Time object.

It is quite often that you will want to pull information out of the Time object to either manipulate it or see if specific criteria are met. You can extract just about any information about the time that you might possibly want.

It is also often the case that you will need to create a Date and Time for a given moment in time. This can be achieved with the static methods gm and local. The gm method will create the time in the “GMT” time zone, whereas local will create the time in your local time zone. There are also the static methods utc and mktime, but these are just a synonyms for gm and local, respectively.

When you construct a Time object, you can provide increasingly specific time components. You cannot, however, skip time components, such as passing the year and the day, but not the month.

An interesting aspect of the Time object is that you can split the object into an array of objects, or create a Time object based upon an array of objects.

While it is possible to perform arithmetic with a Time object, it always assumes the numeric value you add or subtract is seconds. Thus, if you want to add days, weeks, months or years, you’ll have to generate these values based upon seconds. Rails, however, comes to the rescue with the DateTime object along with helpers that make date and time manipulation much easier.

To help you understand when you might want to use Time, Date or DateTime, below is a table of methods that each object responds to, excluding methods that all objects respond to, such as send, is_a?, etc…

MethodTimeDateDateTime
!, !=, !~
+, –
<, <=, <=>,==, ===, =~, >, >=
<<, >>
adj, amjd
asctime
between?
ceil
clamp
ctime
cwday, cweek, cwyear
day
day_fraction
display
downto
dst?
england, italy
eql?, equal?
extend
floor
getgm
getlocal
getutc
gmt?
gmt_offset, gmtoff
gmtime
gregorian, gregorian?
hour
httpdate
infinite?
isdst
iso8601
jd, julian, julian?, mjd
jisx0301
ld, leap?
localtime
marshal_dump, marshal_load
mday
min
minute
mon
monday?, tuesday?, wednesday?,
thursday?, friday?, saturday?, sunday?
month
new_start, start, step
next, succ, next_day, next_month, next_year
nsec
offset, new_offset
prev_day, prev_month, prev_year
rfc2822, rfc, 3339, rfc822
round
sec
sec_fraction, second, second_fraction
strftime
subsec
to_a, to_f, to_i, to_r
:to_date, to_datetime, to_time
tv_nsec, tv_sec, tv_usec
upto
usec
utc, utc?, utc_offset
wday
yday
year
xmlschema
zone