# Parse a Date From a String

**URL:** <https://www.ruby-forum.com/t/parse-a-date-from-a-string/192830>\
**Category:** Ruby\
**Created:** [August 6, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/parse-a-date-from-a-string/192830 "2010-08-06T16:45:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arti\_singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ba9def/32.png) [@arti\_singh](https://www.ruby-forum.com/u/arti_singh)\
**Post date:** [August 6, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/parse-a-date-from-a-string/192830/1 "2010-08-06T16:45:24Z")

</div>

Help!! I get a float when I try to parse a date from a string.  
The date from the string is in the format “Thu Aug 05 15:00:36 2010”  
I try date\_var=“Thu Aug 05 15:00:36 2010”  
date\_var.strptime("%a %b %d %I:%M:%S %Y") but I get a floating point  
variable

---

<div class="post-metadata">

**Author:** ![arti\_singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ba9def/32.png) [@arti\_singh](https://www.ruby-forum.com/u/arti_singh)\
**Post date:** [August 6, 2010, 4:54pm UTC](https://www.ruby-forum.com/t/parse-a-date-from-a-string/192830/2 "2010-08-06T16:54:47Z")

</div>

Try :  
Date.parse(“Thu Aug 05 15:00:36 2010”)  
or  
DateTime.parse(“Thu Aug 05 15:00:36 2010”) if you want to have access to  
hours and minuites

AurÃ©lien AMILIN

> **[holinnn - Overview](https://github.com/holinnn/)**
>
> Freelance software engineer. holinnn has 11 repositories available. Follow their code on GitHub.

Le 06/08/2010 16:45, Arti S. a Ã©crit :

---

<div class="post-metadata">

**Author:** ![arti\_singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ba9def/32.png) [@arti\_singh](https://www.ruby-forum.com/u/arti_singh)\
**Post date:** [August 6, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/parse-a-date-from-a-string/192830/3 "2010-08-06T22:54:04Z")

</div>

Arti S. wrote:

> Help!! I get a float when I try to parse a date from a string.  
> The date from the string is in the format “Thu Aug 05 15:00:36 2010”  
> I try date\_var=“Thu Aug 05 15:00:36 2010”  
> date\_var.strptime("%a %b %d %I:%M:%S %Y") but I get a floating point  
> variable

The “%I” in your code should be a “%H” (24 hour clock)

require ‘date’  
date\_var = “Thu Aug 05 15:00:36 2010”  
date = Date.strptime( date\_var, “%a %b %d %H:%M:%S %Y” )  
p date

hth,

Siep

---

<div class="post-metadata">

**Author:** ![arti\_singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ba9def/32.png) [@arti\_singh](https://www.ruby-forum.com/u/arti_singh)\
**Post date:** [August 7, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/parse-a-date-from-a-string/192830/4 "2010-08-07T17:27:53Z")

</div>

Siep K. wrote:

> Arti S. wrote:
> 
> > Help!! I get a float when I try to parse a date from a string.  
> > The date from the string is in the format “Thu Aug 05 15:00:36 2010”  
> > I try date\_var=“Thu Aug 05 15:00:36 2010”  
> > date\_var.strptime("%a %b %d %I:%M:%S %Y") but I get a floating point  
> > variable
> 
> The “%I” in your code should be a “%H” (24 hour clock)
> 
> require ‘date’  
> date\_var = “Thu Aug 05 15:00:36 2010”  
> date = Date.strptime( date\_var, “%a %b %d %H:%M:%S %Y” )  
> p date
> 
> hth,
> 
> Siep

p(obj) displays obj using its inspect method.  
To display the date use date.to\_s as in;

require ‘date’  
date\_var = “Thu Aug 05 15:00:36 2010”  
date = Date.strptime( date\_var, “%a %b %d %H:%M:%S %Y” )

p date # displays: #\<Date: 4910827/2,0,2299161\>  
p date.to\_s # displays: “2010-08-05” note the double quotes  
puts date.to\_s # displays: 2010-08-05

HTH gfb
