Date from dd-mmm-yyyy

(Chris Roos) #1

Hi,

I've just spent a short while creating a couple of methods that take a string representing a date (in the format dd-mmm-yyyy, e.g. 10-Aug-2005) and return an actual date object.

I did do some research before writing it but couldn't find anything relevant that already did this.

Now that I've done it I wonder if someone could let me know whether there is in fact something that already does this and I've just missed it?

Cheers,

Chris

(Patrick Fernie) #2

The following should do it:

require 'date'

d = Date.strptime("10-Aug-2005", "%d-%b-%Y")
d.strptime("%F") #=> "2005-08-10"

-Patrick

···

On 8/10/05, Chris Roos <chris@seagul.co.uk> wrote:

Hi,

I've just spent a short while creating a couple of methods that take a
string representing a date (in the format dd-mmm-yyyy, e.g. 10-Aug-2005)
and return an actual date object.

I did do some research before writing it but couldn't find anything
relevant that already did this.

Now that I've done it I wonder if someone could let me know whether
there is in fact something that already does this and I've just missed it?

Cheers,

Chris

(Kirk Haines) #3

irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = Date.parse('10-Aug-2005')
=> #<Date: 4907185/2,0,2299161>

Kirk Haines

···

On Wednesday 10 August 2005 10:58 am, Chris Roos wrote:

Hi,

I've just spent a short while creating a couple of methods that take a
string representing a date (in the format dd-mmm-yyyy, e.g. 10-Aug-2005)
and return an actual date object.

(Patrick Fernie) #4

Urp, last line should be:
d.strftime("%F") #=> "2005-08-10"

-P

···

On 8/10/05, Patrick Fernie <patrick.fernie@gmail.com> wrote:

The following should do it:

require 'date'

d = Date.strptime("10-Aug-2005", "%d-%b-%Y")
d.strptime("%F") #=> "2005-08-10"

-Patrick

On 8/10/05, Chris Roos <chris@seagul.co.uk> wrote:
> Hi,
>
> I've just spent a short while creating a couple of methods that take a
> string representing a date (in the format dd-mmm-yyyy, e.g. 10-Aug-2005)
> and return an actual date object.
>
> I did do some research before writing it but couldn't find anything
> relevant that already did this.
>
> Now that I've done it I wonder if someone could let me know whether
> there is in fact something that already does this and I've just missed it?
>
> Cheers,
>
> Chris
>
>
>

(Chris Roos) #5

Kirk Haines wrote:

···

On Wednesday 10 August 2005 10:58 am, Chris Roos wrote:

Hi,

I've just spent a short while creating a couple of methods that take a
string representing a date (in the format dd-mmm-yyyy, e.g. 10-Aug-2005)
and return an actual date object.

irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = Date.parse('10-Aug-2005')
=> #<Date: 4907185/2,0,2299161>

Kirk Haines

Just as I thought, two different ways to achieve in a max of two lines what I had spent time coding up manually. Thanks for your help guys.

Chris