Parse a Date From a String

A

Arti Singh

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
 
A

AMILIN Aurélien

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 Singh a écrit :
 
S

Siep Korteling

Arti said:
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
 
G

Gianfranco Bozzetti

Siep said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top