# Date Conversions

**URL:** <https://www.ruby-forum.com/t/date-conversions/60763>\
**Category:** Ruby\
**Created:** [January 10, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/date-conversions/60763 "2006-01-10T16:21:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [January 10, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/date-conversions/60763/1 "2006-01-10T16:21:27Z")

</div>

There must be a better way of converting dates than this ugly mess ?

def convert\_date format, s  
case format  
when “yymmddhhmmss”  
year = s[0,2].to\_i + 2000  
month= s[2,2].to\_i  
day = s[4,2].to\_i  
hour = s[6,2].to\_i  
minute=s[8,2].to\_i  
second=s[10,2].to\_i  
Time.local(year,month,day,hour,minute,second)  
when “yyyymmddhhmm”  
year = s[0,4].to\_i  
month= s[4,2].to\_i  
day = s[6,2].to\_i  
hour = s[8,2].to\_i  
minute=s[10,2].to\_i  
Time.local(year,month,day,hour,minute)  
when “ddmmyyhhmm”  
day = s[0,2].to\_i  
month= s[2,2].to\_i  
year = s[4,2].to\_i + 2000  
hour = s[6,2].to\_i  
minute=s[8,2].to\_i  
Time.local(year,month,day,hour,minute)  
when “yyyymmdd”  
year = s[0,4].to\_i  
month= s[4,2].to\_i  
day = s[6,2].to\_i  
Time.local(year,month,day)  
end  
end

Christer

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [January 10, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/date-conversions/60763/2 "2006-01-10T16:38:04Z")

</div>

Christer N. wrote:

> ```
> second=s[10,2].to_i
> month= s[2,2].to_i
> 
> ```
> 
> end
> 
> Christer

I’m not too sure about the + 2000 part of the year calculation. It seems  
to be missing the pivot.

The real problem is that there are so many different date formats and  
none of them are _sensible_. You could write some fsm to parse the  
string of “ymdhms” symbols and give yourself a pat on the back for your  
coding skills or you could just use the code and get on with something  
more interesting. As a point of note there is the question of the year  
values, there should be an else clause to raise an error and there is no  
checking that the values are sensible (or even numbers), does  
format.size == s.size etc.

Yes to code looks ugly but I would be more concerned about the possible  
errors than it not being /nice/.

Sometimes ugly is the best you can get.

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [January 10, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/date-conversions/60763/3 "2006-01-10T16:38:05Z")

</div>

Hi –

On Wed, 11 Jan 2006, Christer N. wrote:

> There must be a better way of converting dates than this ugly mess ?

See Date.parse.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [January 10, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/date-conversions/60763/4 "2006-01-10T17:50:35Z")

</div>

David A. Black wrote:

> See Date.parse.

Thank you, David.  
Your hint collapsed my code.  
Christer

def convert\_date format, s  
raise "illegal date " + format + " " + s unless format.size == s.size  
s = “20” + s if format[0,6]==“yymmdd”  
s = “20” + s[4,2] + s[2,2] + s[0,2] + s[6,4] if  
format[0,10]==“ddmmyyhhmm”  
DateTime.parse(s)  
end
