Parse a Date From a String

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

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
http://github.com/holinnn/

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

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

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

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