# Date Format Change - Help request

**URL:** <https://www.ruby-forum.com/t/date-format-change-help-request/93052>\
**Category:** Ruby\
**Created:** [April 27, 2007, 12:26am UTC](https://www.ruby-forum.com/t/date-format-change-help-request/93052 "2007-04-27T00:26:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [April 27, 2007, 12:26am UTC](https://www.ruby-forum.com/t/date-format-change-help-request/93052/1 "2007-04-27T00:26:08Z")

</div>

Newbie question about changing a date format

I have a series of data files in the format:  
ABC, 7/21/2005, 1825  
CNC1, 7/28/2005, 34.21  
BPR, 9/3/2006, 34872

I need to output it to a DIFFERENT Directory as  
ABC, 20050721, 1825  
CNC1, 20050728, 34.21  
BPR, 20060903, 34872

code snippet below

Find.find(sourcedir) do |path|  
if File.file? path  
puts path  
log.puts path  
output = path.gsub(sourcedir, destdir) # name/dir change for  
output file  
data = File.readlines(path)  
data.each { |row|  
(machine, date, produced) = row.split(/,/)  
(tmonth, tday, tyear) = date.split(///)  
newdate = tyear + “%02d” % tmonth + “%02d” % tday  
date = newdate  
log2.puts machine + " , "+ date + " , " + newdate  
}  
log.puts "Outputting file " + output  
open(output,‘w’) {|f| f.puts data}  
end  
end

I get the file output to the proper location, but the DATE format does  
not change.  
The log2 shows output in the proper format, the date and newdate ARE the  
same.  
The output file is still the same as the original input file.

Not sure what I am missing, but any assistance is appreciated.

---

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [April 27, 2007, 3:44am UTC](https://www.ruby-forum.com/t/date-format-change-help-request/93052/2 "2007-04-27T03:44:56Z")

</div>

Hi,

the #each method does not modify the contents of the item that is being  
looped over. I would use #map. You have the data you want to modify on  
this array, so call data.map {|row| …} and do your text transformation  
inside the block. The last line is what counts, so to transform data[]  
as you have described, the last line in the block should be ‘machine + "  
, "+ date + " , " + newdate’, you don’t need “puts” or anything.

Dan

Snoopy D. wrote:

> BPR, 20060903, 34872  
> data.each { |row|
> 
> I get the file output to the proper location, but the DATE format does  
> not change.  
> The log2 shows output in the proper format, the date and newdate ARE the  
> same.  
> The output file is still the same as the original input file.
> 
> Not sure what I am missing, but any assistance is appreciated.

Find.find(sourcedir) do |path|  
if File.file? path  
puts path  
log.puts path  
output = path.gsub(sourcedir, destdir) # name/dir change for  
output file  
data = File.readlines(path)  
data.each { |row|  
(machine, date, produced) = row.split(/,/)  
(tmonth, tday, tyear) = date.split(///)  
newdate = tyear + “%02d” % tmonth + “%02d” % tday  
date = newdate  
log2.puts machine + " , "+ date + " , " + newdate  
}  
log.puts "Outputting file " + output  
open(output,‘w’) {|f|  
data.each{|row|  
(machine, date, produced) = row.split(/,/)  
(tmonth, tday, tyear) = date.split(///)  
newdate = tyear + “%02d” % tmonth + “%02d” % tday  
date = newdate  
f.write  
}  
}  
end  
end

---

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [April 27, 2007, 5:16am UTC](https://www.ruby-forum.com/t/date-format-change-help-request/93052/3 "2007-04-27T05:16:50Z")

</div>

Dan Z. wrote:

> Hi,
> 
> the #each method does not modify the contents of the item that is being  
> looped over. I would use #map. You have the data you want to modify on  
> this array, so call data.map {|row| …} and do your text transformation  
> inside the block. The last line is what counts, so to transform data[]  
> as you have described, the last line in the block should be ‘machine + "  
> , "+ date + " , " + newdate’, you don’t need “puts” or anything.
> 
> Dan

THANK YOU DAN

I ended up using

data.map! (|row|

as the control and added the line  
machine + ", " + newdate + ", " + produced

as the last line in the block to get my desired result.

Thanks for your help
