Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Convert String to date

Reply
Thread Tools

Convert String to date

 
 
aidy
Guest
Posts: n/a
 
      09-01-2006
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

 
Reply With Quote
 
 
 
 
Paul Battley
Guest
Posts: n/a
 
      09-01-2006
On 01/09/06, aidy <> 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?


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

Paul.

 
Reply With Quote
 
 
 
 
aidy
Guest
Posts: n/a
 
      09-01-2006

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

 
Reply With Quote
 
Paul Battley
Guest
Posts: n/a
 
      09-01-2006
On 01/09/06, aidy <> 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.

 
Reply With Quote
 
aidy
Guest
Posts: n/a
 
      09-01-2006
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

 
Reply With Quote
 
Paul Battley
Guest
Posts: n/a
 
      09-01-2006
On 01/09/06, aidy <> 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?


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

Paul.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert date recevied as String to date in local time zone deepak_kamath_n@yahoo.co.in C++ 8 05-01-2007 12:26 PM
Ordinal Date String to Standard Date String for a Beginner davyb Ruby 7 11-18-2005 08:54 PM
how can I convert date infomation to a string just includes the date not the time wgan Java 7 07-08-2004 07:08 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
convert gregorian date to jalay date h_ghanaty ASP .Net 1 11-09-2003 06:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57