create Date from Monthname

H

Hardy Henneberg

Hi,
I have a string of format: '28/Sep/2005'.
I am looking for the easiest way to create a Date from that string.
I don't want to make my own conversion of monthname to monthnumber if
not needed.

regards
Hardy
 
D

David Teare

DateTime.parse works for me when using the format "2005-Oct-12". I
tried with yours, and it doesn't like the slashes, but it was smart
enough to figure out the order of the date:

require 'date'
d = DateTime.parse("28-Sep-2005")
d.to_s => "2005-09-28T00:00:00Z"

So, I would just do a gsub before calling parse. i.e. d =
DateTime.parse("28/Sep/2005".gsub!("/", "-"))

HTH
 
W

WATANABE Hirofumi

Hi,

Hardy Henneberg said:
Hi,
I have a string of format: '28/Sep/2005'.
I am looking for the easiest way to create a Date from that string.
I don't want to make my own conversion of monthname to monthnumber if
not needed.

% ruby -rdate -e 'puts Date.strptime("28/Sep/2005", "%d/%b/%Y")'
2005-09-28
 
H

Hardy Henneberg

Thanks,
that's what I was looking for.
BTW - do you have a link to a site defining these formatting symbols ?

regards Hardy
 
S

Stefan Lang

Thanks,
that's what I was looking for.
BTW - do you have a link to a site defining these formatting
symbols ?

I guess they are the same as those documented under:

% ri Time#strftime

HTH,
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top