[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Glenn Smith

4/13/2005 1:36:00 PM

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



9 Answers

Guillaume Cottenceau

4/13/2005 1:43:00 PM

0

On 4/13/05, Glenn Smith <glenn.ruby@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.h...

But there sure miss some more/better information there.

--
Guillaume Cottenceau - http://zar...



James Gray

4/13/2005 1:46:00 PM

0

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

4/13/2005 1:50:00 PM

0

Hi,

Glenn Smith <glenn.ruby@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

4/13/2005 1:50:00 PM

0

ParseDate? It's in Ruby's Std Lib -
http://www.rubycentral.com/book/lib_standard.html#ParseDate...

Farrel



James Gray

4/13/2005 1:53:00 PM

0

On Apr 13, 2005, at 8:49 AM, WATANABE Hirofumi wrote:

> Hi,
>
> Glenn Smith <glenn.ruby@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

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

James Edward Gray II



Neil Stevens

4/13/2005 1:58:00 PM

0

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 - neil@hakubi.us

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

Glenn Smith

4/13/2005 2:01:00 PM

0

Thanks guys - I'm getting jumpy about my demo tomorrow!
G

On 4/13/05, James Edward Gray II <james@grayproductions.net> wrote:
> On Apr 13, 2005, at 8:49 AM, WATANABE Hirofumi wrote:
>
> > Hi,
> >
> > Glenn Smith <glenn.ruby@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
>
> I knew there had to be a clever solution. Nice!
>
> James Edward Gray II
>
>


--

All the best
Glenn
Aylesbury, UK



Neil Stevens

4/13/2005 2:02:00 PM

0

On Wed, 13 Apr 2005 23:50:12 +0900, Farrel Lifson wrote:

> ParseDate? It's in Ruby's Std Lib -
> http://www.rubycentral.com/book/lib_standard.html#ParseDate...

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'.

--
Neil Stevens - neil@hakubi.us

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

Glenn Smith

4/14/2005 5:04:00 AM

0

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!)

On 4/13/05, Neil Stevens <neil@hakubi.us> wrote:
> On Wed, 13 Apr 2005 23:50:12 +0900, Farrel Lifson wrote:
>
> > ParseDate? It's in Ruby's Std Lib -
> > http://www.rubycentral.com/book/lib_standard.html#ParseDate...
>
> 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'.
>
> --
> Neil Stevens - neil@hakubi.us
>
> 'A republic, if you can keep it.' -- Benjamin Franklin
>
>


--

All the best
Glenn
Aylesbury, UK