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-forum.com/.

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" <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-forum.com/\.

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

···

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

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-forum.com/\.

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

Have we learned nothing from Y2K?

···

On Jun 26, 8:50 pm, Cool Wong <coolwon...@yahoo.com> wrote:

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

Morton Goldberg 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

···

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

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

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

···

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

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