Convert String to date

Hi,

I have a string object

"27/07/06"

that is referenced in this variable: start_date

what I want to do is convert this string into a date, and add a day to
it.

Something like this

start_date = start_date.to_d
end_date = start_date + 1

but I cannot see a to_date method. Could anyone please help?

Aidy

require 'date'
date = Date.strptime(start_date, '%d/%m/%y')
date.year # => 2006
date.month # => 7
date.mday # => 27
(date + 1).mday # => 28

Paul.

···

On 01/09/06, aidy <aidy.rutter@gmail.com> wrote:

I have a string object

"27/07/06"

that is referenced in this variable: start_date

what I want to do is convert this string into a date, and add a day to
it.

Something like this

start_date = start_date.to_d
end_date = start_date + 1

but I cannot see a to_date method. Could anyone please help?

Hi Paul

require 'date'
date = Date.strptime(start_date, '%d/%m/%y')
date.year # => 2006
date.month # => 7
date.mday # => 27
(date + 1).mday # => 28

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}"

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"

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!
    
Using Ruby 184.17
    
Could you please help?
    
Thanks
    
aidy

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.

···

On 01/09/06, aidy <aidy.rutter@gmail.com> wrote:

Hi Paul

And if it's already a Date, there's no need to use strptime!

If I write this

require 'date'
start_date = Date.strptime(start_date, '%d/%m/%y')
end_day = (start_date + 1)
p end_day.to_s #putting to string to see format

I recieve this
=> "2006-06-15"

How then do I format it into a two-digit year European date?

e.g.

"15/06/06"

(I can't see anything in the date class.)

This is what I am doing at the moment

  require 'date'
  start_date = Date.strptime(start_date, '%d/%m/%y')
  end_day = (start_date + 1)
  p end_day.to_s
  end_day = end_day.to_s
  a = end_day.split('-')
  end_day = "#{a[2]}/#{a[1]}/#{a[0]}"
  p end_day
  
  => "15/06/2006"
  
thanks

aidy

end_day.strftime('%d/%m/%y')
=> "15/06/06"

Paul.

···

On 01/09/06, aidy <aidy.rutter@gmail.com> wrote:

p end_day.to_s #putting to string to see format

I recieve this
=> "2006-06-15"

How then do I format it into a two-digit year European date?