Parsing to DateTime

I have to parse input string to DateTime in format "%Y.%m.%d". How can I do it? DateTime.parse() didn't recognize.
Thanks,

Gábor

You're looking for Date.strptime().

Hope that helps.

James Edward Gray II

···

On Jul 19, 2005, at 1:02 PM, Gábor SEBESTYÉN wrote:

I have to parse input string to DateTime in format "%Y.%m.%d". How can I do it? DateTime.parse() didn't recognize.

James Edward Gray II said:

I have to parse input string to DateTime in format "%Y.%m.%d". How
can I do it? DateTime.parse() didn't recognize.

You're looking for Date.strptime().

I also just wrote this up for this special case:

def parse_date(str)
  if str =~ /\A(\d{4})\.(\d{1,2})\.(\d{1,2})\Z/
    Time.local($1,$2,$3,0,0,0)
  end
end

p parse_date("2005.07.19")
p parse_date("2005.7.19")
p parse_date("2005.7.9")

Which you use depends on whether you want a Date or Time.

Ryan

···

On Jul 19, 2005, at 1:02 PM, Gábor SEBESTYÉN wrote:

That was the answer. Thanks!

Gábor

···

On 2005.07.19., at 20:16, James Edward Gray II wrote:

You're looking for Date.strptime().