C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: When this program was run, it was the last day of March. The output shows the correct values.
Month: This returns the numeric form of the month. So January will be equal to 1. And December will be equal to 12.
Day: This returns the numeric day integer. The first day of the month is 1. The last day depends on the month in question.
Year: The year is also stored in integer representation. It contains the full year number, not a shortened form.
Ruby program that uses DateTime.now
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"
Output
Month: 2
Day: 21
Year: 2017
Note: Only integers are supported here. If you try adding or subtracting a floating-point number, the fractional part is ignored.
Ruby program that adds, subtracts days
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.
puts subtract
puts d
puts add
Output
2013-12-31
2014-01-01
2014-01-02
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.
Mktime: This has the same effect as local. If it is easier for you to remember mktime, then you can use it instead.
Ruby program that uses gm, local
# Create a UTC time for the specified values.
t = Time.gm(2013, "mar", 15)
puts t
# Create time with local time zone.
t = Time.local(2010, "apr", 10)
puts t
# More arguments can be passed.
t = Time.gm(2012, "jun", 30, 6, 30, 30, 50)
puts t
Output
2013-03-15 00:00:00 UTC
2010-04-10 00:00:00 -0700
2012-06-30 06:30:30 UTC
Note: For testing leap years, the day and month are not relevant. Just the year is needed.
Ruby program that uses leap method
require "date"
def days_in_year(year)
d = Date.new(year, 1, 1)
# See if the year is a leap year.
if d.leap?
return 366
else
return 365
end
end
# Test years for leap status.
2000.upto(2010) do |year|
puts year.to_s << " has " <<
days_in_year(year).to_s << " days"
end
Output
2000 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
Block: With iterators we specify a block that has a variable in it. With dates, this variable represents an interim day.
IteratorRuby program that uses upto, downto
require "date"
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 d
end
puts "downto"
# Iterate from high to low date.
d2.downto(d1) do |d|
puts d
end
Output
upto
2014-10-05
2014-10-06
2014-10-07
downto
2014-10-07
2014-10-06
2014-10-05
End: 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.
Ruby program that uses step
require "date"
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|
puts d
end
Output
2014-10-01
2014-10-08
2014-10-15
2014-10-22
2014-10-29
Here: These methods return the first day of November. Succ and next roll over from October to November.
Ruby program that uses next, succ
require "date"
d = Date.new(2014, 10, 31)
# Get the next date with succ or next.
d2 = d.succ
puts d2
d2 = d.next
puts d2
Output
2014-11-01
2014-11-01
Ruby program that adds, subtracts months
require "date"
d = Date.new(2015, 5, 1)
# Add two months to the date.
d2 = d >> 2
puts d2
# Subtract three months from the date.
d3 = d << 3
puts d3
Output
2015-07-01
2015-02-01
And: When we call the next method the wday increments, unless it rolls over to 0 again.
Ruby program that uses wday
require "date"
# This is a Sunday, so its number is 0.
d = Date.new(2014, 10, 5)
puts d.wday
# Next day is a Monday (number 1).
d = d.next
puts d.wday
# Tuesday (number 2).
d = d.next
puts d.wday
# One week later is Tuesday again.
d += 7
puts d.wday
Output
0
1
2
2