DateTime problem

C

Cool Wong

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

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

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

Mike Cahill

teach-a-man-to-fish answer: check http://ruby-doc.org/stdlib/ , 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" <[email protected]>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <[email protected]>
Sent: Tuesday, June 26, 2007 8:50 PM
Subject: DateTime problem
 
M

Morton Goldberg

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
 
J

Joel VanderWerf

Morton said:
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
 
M

Morton Goldberg

Morton said:
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
 
J

Joel VanderWerf

Morton said:
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).
 
A

Alin Popa

Cool said:
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
 

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

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,310
Members
47,977
Latest member
MillaDowdy

Latest Threads

Top