DateTime. In Ruby consider the DateTime class. It fetches the current date and relative dates. We use operators to adjust days and months. We can even check for leap years.
Built-in methods. By using methods like DateTime.now, we can get the current time. And properties like month, year, and day can reveal date info.
An example. This program gets the current date with DateTime.now. It also shows how to access the month, day and year parts of the DateTime.
Note When this program was run, it was the last day of March. The output shows the correct values.
Detail This returns the numeric form of the month. So January will be equal to 1. And December will be equal to 12.
Detail This returns the numeric day integer. The first day of the month is 1. The last day depends on the month in question.
Detail The year is also stored in integer representation. It contains the full year number, not a shortened form.
require "date"# Get current date.
current = DateTime.now
# Print current month, day and year.
print "Month: ", current.month, "\n"
print " Day: ", current.day, "\n"
print " Year: ", current.year, "\n"Month: 2
Add, subtract. We can add or subtract an integer from a Date. Subtracting one yields the previous day, and adding one moves to the next day. In this way, we compute yesterday and tomorrow.
Note Only integers are supported here. If you try adding or subtracting a floating-point number, the fractional part is ignored.
require "date"# First day of 2014.
d = Date.new(2014, 1, 1)
# Subtract one day to get the last day of 2013.
subtract = d - 1
# Add one day to get the next day.
add = d + 1
# Display values.
Gm, local. We create arbitrary times with the Time.gm and Time.local methods. Time.gm returns a UTC time. Time.local returns a time adjusted for your local time zone.
Here In this program, I pass the year, month, and day to these methods. These are the first three arguments.
Further We can pass more arguments to Time.gm to increase the accuracy of your time. Here I pass 6.
Detail This has the same effect as local. If it is easier for you to remember mktime, then you can use it instead.
# Create a UTC time for the specified values.
t = Time.gm(2013, "mar", 15)
# Create time with local time zone.
t = Time.local(2010, "apr", 10)
# More arguments can be passed.
t = Time.gm(2012, "jun", 30, 6, 30, 30, 50)
puts t2013-03-15 00:00:00 UTC
2010-04-10 00:00:00 -0700
2012-06-30 06:30:30 UTC
Leap. This method tells us if a year is a leap year or not. Leap years have an extra day. We must first create a Date object with the Date.new initialize method.
Note For testing leap years, the day and month are not relevant. Just the year is needed.
d = Date.new(year, 1, 1)
# See if the year is a leap year.
# Test years for leap status.
2000.upto(2010) do |year|
puts year.to_s << " has " << days_in_year(year).to_s << " days"
end2000 has 366 days
2001 has 365 days
2002 has 365 days
2003 has 365 days
2004 has 366 days
2005 has 365 days
2006 has 365 days
2007 has 365 days
2008 has 366 days
2009 has 365 days
2010 has 365 days
Upto, downto. Dates have helpful iterators. In Ruby we often prefer iterators over loops. For upto, the initial date is incremented upwards, a day at a time. And downto decrements the day.
Detail With iterators we specify a block that has a variable in it. With dates, this variable represents an interim day.
d1 = Date.new(2014, 10, 5)
d2 = Date.new(2014, 10, 7)
puts "upto"# Iterate from low to high date.
d1.upto(d2) do |d|
puts "downto"# Iterate from high to low date.
d2.downto(d1) do |d|
Step. This iterator is more flexible than upto or downto. We specify the "increment" or decrement value. So a step of 7 advances an entire week on each iteration.
Detail Step terminates when the next value reached has gone past the target value.
Also Negative steps are allowed. With a negative step we can go backwards in time.
first = Date.new(2014, 10, 1)
last = Date.new(2014, 11, 1)
# Advance seven days each iteration (these are Wednesdays).
first.step(last, 7) do |d|
Next. The next and succ methods return the successive date—the "tomorrow" date. To me, the next() method is more clearly named. In this program, I specify the end date of October.
Here These methods return the first day of November. Succ and next roll over from October to November.
d = Date.new(2014, 10, 31)
# Get the next date with succ or next.
d2 = d.succ
d2 = d.next
Months. We use shift operators with two greater than (or less than) signs to add (or subtract) months. This changes the month in a date. The plus and minus are used for days.
d = Date.new(2015, 5, 1)
# Add two months to the date.
d2 = d >> 2
# Subtract three months from the date.
d3 = d << 3
Wday. This number indicates the "day of week" or weekday of a date. It returns the numbers 0 through 6, with 0 meaning Sunday and 6 meaning Saturday.
And When we call the next method the wday increments, unless it rolls over to 0 again.
require "date"# This is a Sunday, so its number is 0.
d = Date.new(2014, 10, 5)
# Next day is a Monday (number 1).
d = d.next
# Tuesday (number 2).
d = d.next
# One week later is Tuesday again.
d += 7
With DateTime, we access many date functions. These would be hard to implement. It is typically best to reuse these library methods where possible.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 15, 2023 (edit).