Forum: Ruby convert string to Time?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris McMahon (Guest)
on 2007-01-31 23:21
(Received via mailing list)
How can I convert a string such as '2007-01-31 12:22:26' to a Time
object?
Paul B. (Guest)
on 2007-01-31 23:31
(Received via mailing list)
On Thu, Feb 01, 2007 at 06:20:08AM +0900, Chris McMahon wrote:
> How can I convert a string such as '2007-01-31 12:22:26' to a Time
> object?

irb(main):001:0> require 'time'
=> true
irb(main):002:0> Time.parse('2007-01-31 12:22:26')
=> Wed Jan 31 12:22:26 EST 2007
Ben B. (Guest)
on 2007-01-31 23:34
(Received via mailing list)
On Thu, Feb 01, 2007, Chris McMahon wrote:
> How can I convert a string such as '2007-01-31 12:22:26' to a Time
> object?

Yup!  You want Time.parse: ri Time.parse

Ben
Tom W. (Guest)
on 2007-01-31 23:49
(Received via mailing list)
Chris McMahon wrote:
> How can I convert a string such as '2007-01-31 12:22:26' to a Time
> object?
>
>

Chronic can do that and a whole lot more (in case you need to)!

require 'chronic'

Chronic.parse('2007-01-31 12:22:26')
=> Wed Jan 31 12:22:26 -0800 2007

Chronic.parse('today at 12:22.26')
=> Wed Jan 31 12:22:26 -0800 2007

http://chronic.rubyforge.org

Tom
Florian Aßmann (Guest)
on 2007-02-01 00:48
(Received via mailing list)
Look at http://whytheluckystiff.net/ruby/pickaxe/

... see Time.mktime

... first you should split(' ') ya string and then split the result,
order da stuff and put it into mktime...


Am 31.01.2007 um 22:20 schrieb Chris McMahon:
_Kevin (Guest)
on 2007-02-01 14:56
(Received via mailing list)
On Jan 31, 5:47 pm, Florian Aßmann <removed_email_address@domain.invalid> wrote:
> > object?
the ruby-units gem also has a helper for this...

'2007-01-31 12:22:26'.to_time  #=>  Wed Jan 31 12:22:26 EST 2007

_Kevin
Christopher B. (Guest)
on 2007-02-01 17:25
(Received via mailing list)
On 01 Feb 2007, at 2:55 PM, _Kevin wrote:

>>> How can I convert a string such as '2007-01-31 12:22:26' to a Time
>>> object?
>
> the ruby-units gem also has a helper for this...
>
> '2007-01-31 12:22:26'.to_time  #=>  Wed Jan 31 12:22:26 EST 2007
>
> _Kevin
>
>
Time. parse seems to do the trick too...

irb(main):003:0> Time.parse("2007-01-31 12:22:26")
=> Wed Jan 31 12:22:26 SAST 2007
naveen k. (Guest)
on 2012-04-04 17:29
hi friends..i tring to scrape links using nokogiri gem..i using the
while loop,but the linkis not working properly to while loop the
condition is "status =404"..pls solve my problem
Eric C. (Guest)
on 2012-04-06 00:37
(Received via mailing list)
On Wed, Apr 4, 2012 at 8:29 AM, naveen kumar 
<removed_email_address@domain.invalid>
wrote:
> hi friends..i tring to scrape links using nokogiri gem..i using the
> while loop,but the linkis not working properly to while loop the
> condition is "status =404"..pls solve my problem

You should use double equals in such a condition, e.g. `status ==
404`. Also, the library you're using to fetch web pages might not
store HTTP statuses as numbers; you might have to compare against the
string '404' instead.
Kanna V. (Guest)
on 2014-04-29 22:50
Hi

How to parse time in string

"Friday February seventh, three thirty AM two thousand fourteen"
Joel P. (Guest)
on 2014-04-30 01:41
If the string is always in the same format you can scan it and convert
each section into the appropriate value before feeding it to Time.parse.
This topic is locked and can not be replied to.