# DateTime problem

**URL:** <https://www.ruby-forum.com/t/datetime-problem/107881>\
**Category:** Ruby\
**Created:** [June 27, 2007, 2:50am UTC](https://www.ruby-forum.com/t/datetime-problem/107881 "2007-06-27T02:50:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 2:50am UTC](https://www.ruby-forum.com/t/datetime-problem/107881/1 "2007-06-27T02:50:08Z")

</div>

DateArray = [“Apr”, “2”, “2007”]

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

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

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 3:01am UTC](https://www.ruby-forum.com/t/datetime-problem/107881/2 "2007-06-27T03:01:57Z")

</div>

teach-a-man-to-fish answer: check [RDoc Documentation](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 W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Newsgroups: comp.lang.ruby  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, June 26, 2007 8:50 PM  
Subject: DateTime problem

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 4:23am UTC](https://www.ruby-forum.com/t/datetime-problem/107881/3 "2007-06-27T04:23:06Z")

</div>

On Jun 26, 2007, at 8:50 PM, Cool W. 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?

`
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
` day = 02 month = 04 year = 07

Regards, Morton

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 5:02am UTC](https://www.ruby-forum.com/t/datetime-problem/107881/4 "2007-06-27T05:02:09Z")

</div>

Morton G. wrote:

> FMT

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

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 6:07am UTC](https://www.ruby-forum.com/t/datetime-problem/107881/5 "2007-06-27T06:07:13Z")

</div>

Morton G. 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).

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 5:31am UTC](https://www.ruby-forum.com/t/datetime-problem/107881/6 "2007-06-27T05:31:11Z")

</div>

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

> > FMT
> 
> 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

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 7:45am UTC](https://www.ruby-forum.com/t/datetime-problem/107881/7 "2007-06-27T07:45:00Z")

</div>

Cool W. 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

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 27, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/datetime-problem/107881/8 "2007-06-27T15:31:03Z")

</div>

On Jun 26, 8:50 pm, Cool W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?
