Get the last day of the month given the month - ruby

How would I get the last day of a current month given the month number.
For example if the month is 8 (August 2014), how would I get the last day of the month? (The last day of August is the 31th). The function(8) would return 31.
Thank you for the help.

Using Date::civil:
require 'date'
Date.civil(2014, 8, -1).day
# => 31
According to the documentation:
The month and
the day of month should be a negative or a positive number (as a
relative month/day from the end of year/month when negative).
They should not be zero.

require 'date'
def function(month)
raise "invalid month" unless month.is_a?(Integer) and month.between?(1, 12)
month == 12 ? 31 : Date.new(Date.today.year,month+1,1).prev_day.day
end
function(8)
# => 31
source: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-prev_day

Related

print a calendar for a given date in Ruby

I am trying to create a method that takes user entered month and year and prints out an accurate calendar for the month and year. I am about halfway through but have no idea how to move on.
The guidelines Below in Bold is where i am having issues
Determine the beginning of the next month. Calculate the last day of the month by doing the following: Get the next month date, now get the previous day of that next month. Print the Calendar heading for the full month name and year. Print the header row that contains the abbreviated name of the days of the week. Create an array with a length of 7 and a default value of 4 spaces (" ").
Using the date range from the beginning of the month to the end of the month do the following:
Get the numerical representation of the day of the week using the" %w" formatter and convert to an integer. (This will be the index to the array.)
Use the index to set the day of the month within the array, using the "%d" formatter to get the day of the month.
If index equals 6 OR the date equals the last day of the month, then do the following:
Loop through the array
Print the day of the month
Print a blank line Re-initialize the array so that all values are of 4 spaces
def printMonth(year, month)
now= Date.new(year, month, 1)
a= now >> 1
b= a - 1
puts("Calendar for: #{b.strftime("%B, %Y")}")
puts("")
puts("Sun\tMon\tTue\tWed\tThu\tFri\tSat")
days= Array.new(7," ")
c= b.strftime("%w").to_i
end

Given a Ruby DateTime how can I determine if it represents a day in the current work week?

I have a Ruby DateTime instance and I would like to know it is within the current week (Monday-Sunday). I'm not using Rails.
My guess is that I'd need to get the dates of Monday and Sunday for the current week and test whether my date lies between them.
Date.parse("Monday") # get the Monday date of current week
=> #<Date: 2015-10-26 ((2457322j,0s,0n),+0s,2299161j)>
Date.today.between?(Date.parse("Monday"), Date.parse("Monday") + 7)
=> true
DateTime.now.between?(Date.parse("Monday"),Date.parse("Monday") + 7)
=> true

List Array of Days Between Two Dates

I would like to list an array of days between two dates. I can list an array of months with the code below. How might I change this to show every day between two dates?
require 'date'
date_from = Date.parse('2011-05-14')
date_to = Date.parse('2011-05-30')
date_range = date_from..date_to
date_months = date_range.map {|d| Date.new(d.year, d.month, 1) }.uniq
date_months.map {|d| d.strftime "%d/%m/%Y" }
puts date_months
I don't know which day you meant, thus I have shown all the ways.
#wday is the day of week (0-6, Sunday is zero).
(date_from..date_to).map(&:wday)
#mday is the day of the month (1-31).
(date_from..date_to).map(&:mday)
#yday is the day of the year (1-366).
(date_from..date_to).map(&:yday)
OP's actual need was not much clear to me. After few comments between us, I came to know from OP's comment, the below answer OP is looking for -
(date_from..date_to).map(&:to_s)

How can I compare a date field with date.today and display the differance?

I have a table of accounts, each with an expiration date. The expiration date is saved in a date field (day, month and year).
How can I compare this to the current date and display the number of days until the expiration date is reached?
Thanks for any help it's much appreciated!
simple (Date.today - account.expiration_date).to_i will give you the integer number - the difference in days :)
You can convert your components into a target date and use some of the methods in DateHelper to display a human readable distance:
target_date = Date.new(model.year, model.month, model.day)
distance_of_time_in_words(target_date, Date.today)
=> "4 months"

How can I create DateTime object based on the day?

I want to create an Datetime object based on the number of day in the year.
This number is from the 365 days of the year (for example it can be: 123 or 23 or 344...)
How can I do that?
Thanks
Use the DateTime.ordinal method. Here's an example to get the 100th day of year 2011.
require 'date'
year, day = 2011, 100
DateTime.ordinal(year, day)
# #<DateTime: 2011-04-10T00:00:00+00:00 (4911323/2,0,2299161)>
If you want it as the number of days from now you should do the following:
time = Time.new + (60*60*24)*(numberOfDaysFromNow)
If you want it as the number of days from the start of the year you should do the following
time = Time.new(Time.now.year) + (60*60*24)*(dayOfTheYear-1)

Resources