[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

DateTime problem

Cool Wong

6/27/2007 12:50:00 AM

DateArray = ["Apr", "2", "2007"]

How can i read the month and year in the array???

The OUTPUT:
day = 01
month = 04
year = 07

--
Posted via http://www.ruby-....

7 Answers

Mike Cahill

6/27/2007 1:01:00 AM

0

teach-a-man-to-fish answer: check http://ruby-doc.o... , under Date

quick answer:
d = Date.civil(2007,4,2)
puts "OUTPUT:\n day = #{d.day}\n month=#{d.month}\n year=#{d.year}"

also try out puts d.strftime("%m %d %Y")


----- Original Message -----
From: "Cool Wong" <coolwong85@yahoo.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Tuesday, June 26, 2007 8:50 PM
Subject: DateTime problem


> DateArray = ["Apr", "2", "2007"]
>
> How can i read the month and year in the array???
>
> The OUTPUT:
> day = 01
> month = 04
> year = 07
>
> --
> Posted via http://www.ruby-....
>

Morton Goldberg

6/27/2007 2:23:00 AM

0

On Jun 26, 2007, at 8:50 PM, Cool Wong wrote:

> DateArray = ["Apr", "2", "2007"]
>
> How can i read the month and year in the array???
>
> The OUTPUT:
> day = 01
> month = 04
> year = 07

Something like this?

<code>
require "ParseDate"
DA = ["Apr", "2", "2007"]
args = ParseDate.parsedate("#{DA[1]} #{DA[0]} #{DA[2]}")
date = Time.local(*args).strftime(<<FMT)
\tday = %d
\tmonth = %m
\tyear = %y
FMT
puts date
</code>

<result>
day = 02
month = 04
year = 07
</resutl>

Regards, Morton



Joel VanderWerf

6/27/2007 3:01:00 AM

0

Morton Goldberg wrote:
> On Jun 26, 2007, at 8:50 PM, Cool Wong wrote:
>
>> DateArray = ["Apr", "2", "2007"]
>>
>> How can i read the month and year in the array???
>>
>> The OUTPUT:
>> day = 01
>> month = 04
>> year = 07
>
> Something like this?
>
> <code>
> require "ParseDate"
> DA = ["Apr", "2", "2007"]
> args = ParseDate.parsedate("#{DA[1]} #{DA[0]} #{DA[2]}")
> date = Time.local(*args).strftime(<<FMT)
> \tday = %d
> \tmonth = %m
> \tyear = %y
> FMT
> puts date
> </code>
>
> <result>
> day = 02
> month = 04
> year = 07
> </resutl>
>
> Regards, Morton
>

Same idea, but slightly simpler...

da = ["Apr", "2", "2007"]
time = Time.parse(da.join(" "))
puts time.strftime(<<FMT)
\tday = %d
\tmonth = %m
\tyear = %y
FMT

__END__

Output:

day = 02
month = 04
year = 07

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Morton Goldberg

6/27/2007 3:30:00 AM

0

On Jun 26, 2007, at 11:00 PM, Joel VanderWerf wrote:

> Morton Goldberg wrote:
>> Something like this?
>> <code>
>> require "ParseDate"
>> DA = ["Apr", "2", "2007"]
>> args = ParseDate.parsedate("#{DA[1]} #{DA[0]} #{DA[2]}")
>> date = Time.local(*args).strftime(<<FMT)
>> \tday = %d
>> \tmonth = %m
>> \tyear = %y
>> FMT
>> puts date
>> </code>
>> <result>
>> day = 02
>> month = 04
>> year = 07
>> </resutl>
>> Regards, Morton
>
> Same idea, but slightly simpler...
>
> da = ["Apr", "2", "2007"]
> time = Time.parse(da.join(" "))
> puts time.strftime(<<FMT)
> \tday = %d
> \tmonth = %m
> \tyear = %y
> FMT
>
> __END__
>
> Output:
>
> day = 02
> month = 04
> year = 07

Yes, that's better. But don't you need a 'require "time"' at the
beginning? I needed it to make it work in my somewhat obsolescent
version of Ruby. Is it now part of the standard library?

Regards, Morton

Joel VanderWerf

6/27/2007 4:06:00 AM

0

Morton Goldberg wrote:
> Yes, that's better. But don't you need a 'require "time"' at the
> beginning? I needed it to make it work in my somewhat obsolescent
> version of Ruby. Is it now part of the standard library?

I think my irb required time ... (1.8.6).

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Alin Popa

6/27/2007 5:45:00 AM

0

Cool Wong wrote:
> DateArray = ["Apr", "2", "2007"]
>
> How can i read the month and year in the array???
>
> The OUTPUT:
> day = 01
> month = 04
> year = 07

Hi,

For me worked like that:

tt = Time.parse(da.join(" "))
day = tt.strtime("%d")
month = tt.strftime("%m")
year = tt.strftime("%y")

print "\nday #{day}" "\nmonth #{month}"
"\nyear #{year}" "\n"

Best regards,

Alin

--
Posted via http://www.ruby-....

Karl von Laudermann

6/27/2007 1:28:00 PM

0

On Jun 26, 8:50 pm, Cool Wong <coolwon...@yahoo.com> wrote:
>
> The OUTPUT:
> day = 01
> month = 04
> year = 07

I think you mean:
day = 01
month = 04
year = 2007

Have we learned nothing from Y2K?