Paul Battley
9/1/2006 1:48:00 PM
On 01/09/06, aidy <aidy.rutter@gmail.com> wrote:
> Thanks for getting back.
>
> I have extrapolated your example (see below)
>
> require 'date'
> start_date = Date.strptime(start_date, '%d/%m/%Y')
> end_day = (start_date + 1).mday
> end_month = (start_date).mon
> end_year = (start_date).year
> end_date = Date.new(end_year, end_month, end_day)
> p "the end_date is: #{end_date}"
You missed a simple point: adding one to a date gives you the next day:
require 'date'
start_date = '27/07/06'
start_date = Date.strptime(start_date, '%d/%m/%y')
end_date = start_date + 1
puts "The end date is #{ end_date }."
outputs:
The end date is 2006-07-28.
> However, I am having a couple of difficulties
>
> 1) Now for some reason the year '2006' is putting to the console
>
> "the end_date is: 0006-07-28"
That's because you used %Y, not %y, in strptime. If you are parsing a
two-digit year, you need to use %y to infer the century automatically.
%Y reads it verbatim: 06 is 0006, not 2006.
> 2) And if I add this line of code
>
> end_date = Date.strptime(end_date, '%d/%m/%Y')
>
> I receive the error the #strptime is a private method!
Is the first parameter that you pass to strptime a String or a Date?
If it's the Date calculated in the earlier example, that will fail
with an error 'private method `sub!' called for #<Date:...>'. And if
it's already a Date, there's no need to use strptime!
Paul.