Forum: Ruby i have some challenges with the date

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5044d0567cc7ae2e52cbd511958e2c15?d=identicon&s=25 Junkone (Guest)
on 2009-01-05 17:11
(Received via mailing list)
here i have a string date like this. 02/01/2009 : Format is DD/MM/YY
however my date.parse switches the month and the day.
how do i fix it.

irb(main):011:0> Date.parse("02/01/2009").strftime('%Y-%m-%d')
=> "2009-02-01"
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-01-05 17:16
(Received via mailing list)
On Jan 5, 2009, at 10:10 AM, Junkone wrote:

> here i have a string date like this. 02/01/2009 : Format is DD/MM/YY
> however my date.parse switches the month and the day.
> how do i fix it.
>
> irb(main):011:0> Date.parse("02/01/2009").strftime('%Y-%m-%d')
> => "2009-02-01"

You can specify a format for the parse:

 >> Date.strptime("02/01/2009", "%d/%m/%Y").to_s
=> "2009-01-02"

Hope that helps.

James Edward Gray II
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-01-05 17:18
(Received via mailing list)
Junkone wrote:
> here i have a string date like this. 02/01/2009 : Format is DD/MM/YY
> however my date.parse switches the month and the day.
> how do i fix it.
>
> irb(main):011:0> Date.parse("02/01/2009").strftime('%Y-%m-%d')
> => "2009-02-01"

>> Date.strptime("02/01/2009","%d/%m/%Y").to_s
=> "2009-01-02"

HTH,
Sebastian
This topic is locked and can not be replied to.