Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2443 > unrolled thread

convert string to datetime

Started byFan Jin <jeff_yq@yahoo.com.au>
First post2011-04-07 05:12 -0500
Last post2011-04-07 20:55 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  convert string to datetime Fan Jin <jeff_yq@yahoo.com.au> - 2011-04-07 05:12 -0500
    Re: convert string to datetime Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-07 05:22 -0500
      Re: convert string to datetime Brian Candler <b.candler@pobox.com> - 2011-04-07 05:55 -0500
        Re: convert string to datetime Fan Jin <jeff_yq@yahoo.com.au> - 2011-04-07 20:55 -0500

#2443 — convert string to datetime

FromFan Jin <jeff_yq@yahoo.com.au>
Date2011-04-07 05:12 -0500
Subjectconvert string to datetime
Message-ID<0118058fc84316aaefb7bbfddd395421@ruby-forum.com>
Hi folks:
Can anybody tell me how to convert a string format like "20110309" to a
datetime format as "m%/d%/y%"? Thanks.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#2444

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-04-07 05:22 -0500
Message-ID<BANLkTik29wYYDpO32O7QCuA0npRXipfDAA@mail.gmail.com>
In reply to#2443
On Thu, Apr 7, 2011 at 12:12 PM, Fan Jin <jeff_yq@yahoo.com.au> wrote:
> "20110309"

DateTime#parse:

http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html#M000485


ruby-1.8.7-p334 :001 > d = "20110309"
 => "20110309"
ruby-1.8.7-p334 :002 > require 'date'
 => true
ruby-1.8.7-p334 :003 > date = DateTime.parse d
 => #<DateTime: 4911259/2,0,2299161>
ruby-1.8.7-p334 :004 > date.to_s
 => "2011-03-09T00:00:00+00:00"

Jesus.

[toc] | [prev] | [next] | [standalone]


#2445

FromBrian Candler <b.candler@pobox.com>
Date2011-04-07 05:55 -0500
Message-ID<6661754cd57dcfdfe89fc5a6aaddb1f9@ruby-forum.com>
In reply to#2444
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post 
#991424:
> DateTime#parse:

Or DateTime.strptime, if you want to be explicit about the format.

>> DateTime.strptime("20110309","%Y%m%d")
=> #<DateTime: 4911259/2,0,2299161>

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2493

FromFan Jin <jeff_yq@yahoo.com.au>
Date2011-04-07 20:55 -0500
Message-ID<d9561b6421b4de45058fd28b9683a813@ruby-forum.com>
In reply to#2445
Brian Candler wrote in post #991430:
> "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
> #991424:
>> DateTime#parse:
>
> Or DateTime.strptime, if you want to be explicit about the format.
>
>>> DateTime.strptime("20110309","%Y%m%d")
> => #<DateTime: 4911259/2,0,2299161>

Thanks mate, it works :)

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web