formating date information

R

rob stanton

Very new to this so can anyone tell me how to change the date format
from 20100901 to something like 01-09-2010 ?
I have a number of dates like => [["20100824"], ["20100825"],
["20100826"]],

Date.parse("20100824").strftime("%d %b %Y") # thanks Robert
=> "24 Aug 2010"

but how do I parse each date in turn and get a new array with the
standard date format ???

I have looked in a lot of places but can't find the answer.
Thanks for any help...
 
S

Steve Klabnik

[Note: parts of this message were removed to make it a legal post.]
but how do I parse each date in turn and get a new array with the
standard date format ???

What you want is Array#map:

ruby-1.9.2-p0 :007 > a.map {|e| Date.parse(e.first).strftime("%d %b %Y") }
=> ["24 Aug 2010", "25 Aug 2010", "26 Aug 2010"]

(I'm not sure why each of your dates is in a single array, but that's why
you need that 'first'.)
 
R

Robert Klemme

Very new to this so can anyone tell me how to change the date format
from 20100901 to something like 01-09-2010 ?
I have a number of dates like => [["20100824"], ["20100825"],
["20100826"]],

Your Array should really contain Date instances and not String
instances. Usually parsing is done when the data is read from some
external source (command line, file, prompt) so you can continue to work
with proper methods of the particular type.
Date.parse("20100824").strftime("%d %b %Y") # thanks Robert
=> "24 Aug 2010"

but how do I parse each date in turn and get a new array with the
standard date format ???

Not sure what you mean. Is it this?

irb(main):003:0> Date.strptime("24 Aug 2010", "%d %b %Y")
=> #<Date: 2010-08-24 (4910865/2,0,2299161)>
irb(main):004:0> Date.strptime("24 Aug 2010", "%d %b %Y").to_s
=> "2010-08-24"

Kind regards

robert
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,241
Latest member
Lisa1997

Latest Threads

Top