# Converting date format (CSV to yaml)

**URL:** <https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645>\
**Category:** Ruby\
**Created:** [February 2, 2011, 11:13pm UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645 "2011-02-02T23:13:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 2, 2011, 11:13pm UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/1 "2011-02-02T23:13:49Z")

</div>

Hi,

Anyone know how to convert date format of .csv to . yaml?

.csv date format:

12/1/2011

.yaml date format:

2011-01-12

Nizam

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 12:51am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/2 "2011-02-03T00:51:01Z")

</div>

> Anyone know how to convert date format of .csv to . yaml?
> 
> .csv date format:
> 
> 12/1/2011
> 
> .yaml date format:
> 
> 2011-01-12

cdate = “12/1/2011”  
m, d, y = cdate.split(‘/’).map{|i| if i.length == 1 then “0#{i}” else i  
end}  
ydate = “#{y}-#{d}-#{m}”

The map part on the second line takes care of padding a single digit  
month, and date with a ‘0’.

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 1:47am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/3 "2011-02-03T01:47:46Z")

</div>

> Anyone know how to convert date format of .csv to . yaml?
> 
> .csv date format:
> 
> 12/1/2011
> 
> .yaml date format:
> 
> 2011-01-12

Another option could be to parse the String into a Date that is then  
naturally marshaled into the right format. That could help make the  
intent of the code more clear than splitting and re-arranging by hand,  
but note that Date.parse changed from Ruby 1.8 to Ruby 1.9.

Ruby 1.8  
\>\> require ‘date’  
=\> true  
\>\> require ‘yaml’  
=\> true  
\>\> d = Date.parse(‘12/1/2011’)  
=\> #\<Date: 4911793/2,0,2299161\>  
\>\> puts d.to\_yaml  
— 2011-12-01  
=\> nil

Ruby 1.9  
\>\> require ‘date’  
=\> true  
\>\> require ‘yaml’  
=\> true  
\>\> d = Date.parse(‘12/1/2011’)  
=\> #\<Date: 2011-01-12 (4911147/2,0,2299161)\>  
\>\> puts d.to\_yaml  
— 2011-01-12  
=\> nil

In practice, I sometimes just split it by hand, but I usually try to  
use strptime:

Ruby 1.8 and 1.9:  
\>\> d = Date.strptime(‘12/1/2011’, ‘%d/%m/%Y’)  
=\> #\<Date: 2011-01-12 (4911147/2,0,2299161)\>  
\>\> puts d.to\_yaml  
— 2011-01-12  
=\> nil

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 5:50am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/4 "2011-02-03T05:50:05Z")

</div>

On 02/02/2011 10:36 PM, Kamarulnizam R. wrote:

> “2011-01-12”
> 
> How i change it to:  
> 2011-01-12 ← without the quotation mark

The pp method is adding the quotation marks when printing the string.  
The quotes are not really in the string’s value. Try using puts instead  
of pp to see the difference.

-Jeremy

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 6:14am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/5 "2011-02-03T06:14:01Z")

</div>

On 02/02/2011 11:02 PM, Kamarulnizam R. wrote:

> My yaml output:
> 
> - type: TargetComponent  
> title: None  
> args:  
> :start\_date: “2010-08-10”  
> :end\_date: “2011-01-12”  
> :state: Active

It’s because you actually have a String rather than a Date object. Try  
this in irb:

ruby-1.9.2-p136 :001 \> require ‘yaml’  
=\> true  
ruby-1.9.2-p136 :002 \> require ‘date’  
=\> false  
ruby-1.9.2-p136 :003 \> puts Date.today.to\_yaml  
— 2011-02-02  
=\> nil  
ruby-1.9.2-p136 :004 \> puts “2011-02-02”.to\_yaml  
— “2011-02-02”  
=\> nil

The command at :004 is what you’re doing, but you want what the command  
at :003 is doing. Take the advice provided in the post linked below and  
use Date objects to process your dates instead of Strings:

[http://www.ruby-forum.com/topic/1005964#979296](http://www.ruby-forum.com/topic/1005964#979296)

-Jeremy

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 6:02am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/6 "2011-02-03T06:02:49Z")

</div>

But when i tried transferring the ydate into my yaml structure, it still  
print out the quotation marks.

{“type”=\>“TargetComponent”,  
“title”=\>“None”,  
“args”=\>  
{:start\_date=\>‘2010-08-10’,  
:end\_date=\> (ydate),  
:state=\>"#{status}"}}]}

My yaml output:

- type: TargetComponent  
title: None  
args:  
:start\_date: “2010-08-10”  
:end\_date: “2011-01-12”  
:state: Active

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 6:31am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/7 "2011-02-03T06:31:00Z")

</div>

I used both method. Both of them return an error

```
 due_date = 12/1/2011
 date = Date.parse(due_date)
 #date = Date.strptime(due_date, '%d/%m/%Y')

```

C:/Ruby192/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags': invalid date (ArgumentError) from C:/Ruby192/lib/ruby/1.9.1/date.rb:1066:in`parse’

```
 due_date = 12/1/2011
 #date = Date.parse(due_date)
 date = Date.strptime(due_date, '%d/%m/%Y')

```

C:/Ruby192/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags': invalid date (ArgumentError) from C:/Ruby192/lib/ruby/1.9.1/date.rb:1046:in`strptime’

Is it because of my IDE?

Nizam

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 6:54am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/8 "2011-02-03T06:54:17Z")

</div>

On Thu, Feb 3, 2011 at 6:14 AM, Kamarulnizam R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> .csv date format:
> 
> 12/1/2011
> 
> .yaml date format:
> 
> 2011-01-12

when csv reads the data, it’s still a string. you may want to convert  
the string further to Date. in that case, use Date.strptime and pass  
the string and it’s current format

eg in irb,

> Date.strptime “12/1/2011”, “%d/%M/%Y”  
> #=\> #\<Date: 2011-01-12 (4911147/2,0,2299161)\>

where %d stands for day, %M for month, and %Y for year  
note, the format must match your string otherwise you’ll get errors  
like invalid date..

best regards -botp

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 6:55am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/9 "2011-02-03T06:55:01Z")

</div>

problem solved. thanks guys!!!

---

<div class="post-metadata">

**Author:** ![mabd068](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@mabd068](https://www.ruby-forum.com/u/mabd068)\
**Post date:** [February 3, 2011, 5:36am UTC](https://www.ruby-forum.com/t/converting-date-format-csv-to-yaml/202645/10 "2011-02-03T05:36:35Z")

</div>

Hi,

I used the following code:

cdate = “2011-01-12”  
m, d, y = cdate.split(’/’).map{|i| if i.length == 1 then “0#{i}” else i  
end}  
pp ydate = “#{y}-#{d}-#{m}”

Output:  
“2011-01-12”

How i change it to:  
2011-01-12 \<-- without the quotation mark

Nizam
