views:

234

answers:

2

I need to parse following String into a DateTime Object:
30/Nov/2009:16:29:30 +0100

Is there an easy way to do this?

+3  A: 

DateTime.strptime allows you to specify the format and convert a String to a DateTime.

Kaleb Brasee
thanks. Missed, that I could give it my own format. Here's the working one: '%d/%b/%Y:%H:%M:%S'
SkaveRat
A: 

in Ruby 1.8, the ParseDate module will convert this and many other date/time formats. However, it does not deal gracefully with the colon between the year and the hour. Assuming that colon is a typo and is actually a space, then:

#!/usr/bin/ruby1.8

require 'parsedate'

s = "30/Nov/2009 16:29:30 +0100"
p Time.mktime(*ParseDate.parsedate(s))    # =>  Mon Nov 30 16:29:30 -0700 2009
Wayne Conrad