Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Really quick question - How do I convert a string to a date

2 views
Skip to first unread message

Glenn Smith

unread,
Apr 13, 2005, 9:36:10 AM4/13/05
to
I need a 'Date' object which is converted from a string value
containing something like '13/04/2005' (english format date).

Help!

--

All the best
Glenn
Aylesbury, UK

Guillaume Cottenceau

unread,
Apr 13, 2005, 9:42:42 AM4/13/05
to
On 4/13/05, Glenn Smith <glenn...@gmail.com> wrote:
> I need a 'Date' object which is converted from a string value
> containing something like '13/04/2005' (english format date).

Pleac?

http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html#AEN153

But there sure miss some more/better information there.

--
Guillaume Cottenceau - http://zarb.org/~gc/

James Edward Gray II

unread,
Apr 13, 2005, 9:45:30 AM4/13/05
to
On Apr 13, 2005, at 8:36 AM, Glenn Smith wrote:

> I need a 'Date' object which is converted from a string value
> containing something like '13/04/2005' (english format date).
>
> Help!

This isn't a particularly brilliant answer, so hopefully someone will
do better:

ruby -r date -e 'puts Date.parse("13/04/2005".split("/").values_at(1,
0, 2).join("/"))'

Hope that helps.

James Edward Gray II

WATANABE Hirofumi

unread,
Apr 13, 2005, 9:49:55 AM4/13/05
to
Hi,

Glenn Smith <glenn...@gmail.com> writes:

> I need a 'Date' object which is converted from a string value
> containing something like '13/04/2005' (english format date).

% ruby -rdate -e 'puts Date.strptime("13/04/2005", "%d/%m/%Y")'
2005-04-13

--
eban


Farrel Lifson

unread,
Apr 13, 2005, 9:50:12 AM4/13/05
to

James Edward Gray II

unread,
Apr 13, 2005, 9:53:23 AM4/13/05
to

I knew there had to be a clever solution. Nice!

James Edward Gray II

Neil Stevens

unread,
Apr 13, 2005, 9:57:41 AM4/13/05
to
On Wed, 13 Apr 2005 23:36:10 +0900, Glenn Smith wrote:

> I need a 'Date' object which is converted from a string value
> containing something like '13/04/2005' (english format date).
>
> Help!

If you're certain it's that date format:

require 'date'
parts = '13/04/2005'.split('/').reverse
parts = parts.collect do |i|
i = i.to_i
end
date = Date.new(*parts)

--
Neil Stevens - ne...@hakubi.us

'A republic, if you can keep it.' -- Benjamin Franklin

Glenn Smith

unread,
Apr 13, 2005, 10:01:08 AM4/13/05
to
Thanks guys - I'm getting jumpy about my demo tomorrow!
G

Neil Stevens

unread,
Apr 13, 2005, 10:01:33 AM4/13/05
to
On Wed, 13 Apr 2005 23:50:12 +0900, Farrel Lifson wrote:

Nope, that assumes month/day/year when it sees that style. It can only
support one or the other thanks to cases like '1/2/2005'.

Glenn Smith

unread,
Apr 14, 2005, 1:03:36 AM4/14/05
to
It was exactly this problem I was having Neil (ie. using ParseDate on
'13/04'2005' gave me an error - I think it didn't like there being 13
months!)

0 new messages