Some date standards in Ruby

  1. rfc822   ‘Tue, 1 Jul 2003 10:52:37 +0200’
  2. rfc2616 (httpdate)  Tue, 05 Sep 2006 16:05:51 GMT
    podcast use this.
    d.strftime(“%a, %d %b %Y %H:%M:%S %z”)
  3. iso8601 2001-04-17T19:23:17.201Z

code demo:

t = Time.at(1000000000) # => Sat Sep 08 21:46:40 EDT 2001
t.rfc822 # => “Sat, 08 Sep 2001 21:46:40 -0400”
t.httpdate # => “Sun, 09 Sep 2001 01:46:40 GMT”
t.iso8601 # => “2001-09-08T21:46:40-04:00”

Parse string to date

Say you have a string ‘080430’, you can not simply use

Date::strptime(s, ‘%y/%m/%d’)

to convert it to a Date type. (ArgumentError: 3 elements of civil date are necessary)

Had to split to to 3 parts, then do the convert.

s = ‘080204’
s.sub!(/^(\d{2})(\d{2})(\d{2})$/, ‘\1/\2/\3’)
d = Date::strptime(s, ‘%y/%m/%d’)
How to convert a Date to Time?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s