Formating date information

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…

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’.)

On 08.03.2011 22:45, rob stanton wrote:

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

Thanks Steve that worked very well, just what I needed!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs