How do I create a Ruby date object from a string? - ruby

How do I create a Ruby date object from the following string?
DD-MM-YYYY

Date.parse('31-12-2010')
Alternatively Date#strptime(str, format).

Because in the USA they get the dates backwards, it's important not to simply use Date.parse() because you'll find 9/11/2001 can be 11 September 2001 in the USA and 9 November 2001 in the rest of the world. To be completely unambiguous use Date::strptime(your_date_string,"%d-%m-%Y") to correctly parse a date string of format dd-mm-yyyy.
Try this to be sure:
>irb
>> require 'date'
=> true
>> testdate = '11-09-2001'
=> "11-09-2001"
>> converted = Date::strptime(testdate, "%d-%m-%Y")
=> #<Date: 4918207/2,0,2299161>
>> converted.mday
=> 11
>> converted.month
=> 9
>> converted.year
=> 2001
For other strptime formats see http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html
Also I always make sure I set my base timezone to :utc if my website is going to be handling any dates, and use Javascript on the client side to display local times.

You can use Time#parse.
Time.parse("20-08-2010")
# => Fri Aug 20 00:00:00 +0200 2010
However, because Ruby could parse the date as "MM-DD-YYYY", the best way is to go with DateTime#strptime where you can specify the input format.

If you have control over the format of the date in the string, then Date.parse works fine internationally with strings in YYYY-MM-DD (ISO 8601) format:
Date.parse('2019-11-20')

I find this approach simpler since it avoid having to specify the date format for the parser:
date1 = Time.local(2012, 1, 20, 12, 0, 0).to_date

Like this You can get time Object from a string like this:
t = Time.parse "9:00 PM"
=> 2013-12-24 21:00:00 +0530
t = Time.parse "12:00 AM"
=> 2013-12-24 00:00:00 +0530
But Ruby parsing this as a Date!
So you can use the column as a string.
add_column :table_name, :from, :string, :limit => 8, :default => "00:00 AM", :null => false
add_column :table_name, :to, :string, :limit => 8, :default => "00:00 AM", :null => false
And you can assign string object to the attribute,
r.from = "05:30 PM"
r.save
And parse the string for getting time object,
Time.zone.parse("02:00 PM")

Not necessary for this particular string format, but best string to time parsing utility I know is Chronic which is available as a gem and works for about 99.9% of usecases for human formatted dates/times.

Related

how to convert string to date object in ruby

I am getting a date as a string like below:
"September 1998"
I tried like Date.parse("September 1998"), but it did not work.
How do I convert it into a ruby date object which returns string in above format?
Date.strptime('September 1998', '%B %Y'). However, this will represent September 1st 1998, because date objects represent, well, dates.
You could use the chronic gem:
require 'chronic'
t = Chronic.parse('September 1998', :guess => true) #returns a Time object
=> 1998-09-01 00:00:00 -0700
t.to_date #convert to Date object
=> <Date: 1998-09-16 ((2451073j,0s,0n),+0s,2299161j)>
Chronic was created by Tom Preston-Werner, who also co-created Github.
Just prepend the missing "1 ":
str ="September 1998"
p Date.parse("1 " + str) # => #<Date: 1998-09-01 ((2451058j,0s,0n),+0s,2299161j)>

ruby parse date using strptime and strftime

So I'm parsing this string from AIX's errpt - to convert it into epoch - and it doesn't seem to be respecting the Hour and Minute part of the string.
So the string is: 1108095913 (MMDDHHMMYY) .. but when I do my strptime to convert it to a date object, and then format it how I want, it completely zero'd out my hour and minute.
Am I missing something?
irb(main):039:0> Date.strptime("1108095913", "%m%d%H%M%y").strftime('%m/%d/%y %H:%M')
=> "11/08/13 00:00"
You should use Time.strptime instead of Date method, Date removes hours and minutes
1.9.3-p429 :005 > Time.strptime("1108095913", "%m%d%H%M%y").strftime('%m/%d/%y %H:%M')
=> "11/08/13 09:59"
Use DateTime instead of Date:
irb(main):002:0> require 'date'
=> true
irb(main):003:0> DateTime.strptime("1108095913", "%m%d%H%M%y").strftime('%m/%d/%y %H:%M')
=> "11/08/13 09:59"
The reason is DateTime handles date and time, both and Date only handles date.
Hope this helps!

Parsing a string to a date in Ruby

I know this sounds like a repeated question but I think this situation is different. I will delete this post if it truly is.
I have a string containing a date in the following format: Thu, Jun. 20
I would like to parse this into a Date variable and then increment it to the next day.
So far I have done
text = "Thu, Jun. 20"
date = Date.new
date = Date.strptime(text, '{%a, %m, %d}')
But this gives me the following error:
invalid date (ArgumentError)
I got this idea from: Ruby: convert string to date
All answers I have seen so far have been parsing strings that contain full information (the full month or day of the week). Is what I'm trying to do even possible ? If not any suggestions on a work around would be most appreciated.
You used wrong date format. After parse it, you can use plus or minus operator to change date.
Reference: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-i-strftime
%a - The abbreviated name (``Sun'')
%b - The abbreviated month name (``Jan'')
%d - Day of the month, zero-padded (01..31)
Code:
1.9.3p392 :003 > require 'date'
=> true
1.9.3p392 :008 > date = Date.strptime(text, '%a, %b. %d')
=> #<Date: 2013-06-20 ((2456464j,0s,0n),+0s,2299161j)>
1.9.3p392 :009 > date + 1
=> #<Date: 2013-06-21 ((2456465j,0s,0n),+0s,2299161j)>
1.9.3p392 :010 > date - 1
=> #<Date: 2013-06-19 ((2456463j,0s,0n),+0s,2299161j)>
To answer your need I would like to parse this into a Date variable and then increment it to the next day. I tried below :
require 'date'
d = Date.parse("Thu, Jun. 20")
# => #<Date: 2013-06-20 ((2456464j,0s,0n),+0s,2299161j)>
d.to_s # => "2013-06-20"
d.next.to_s # => "2013-06-21"

Time.parse produces strange dates

Wondering what happened with these inputs that gave me something strange instead of 2013-05-31 13:30:00 -0400
Time.parse("05-31 13:30") => 2013-06-06 16:30:00 -0400
Time.parse("5 31 13:30") => 2013-07-01 13:30:00 -0400
#SergioTulentsev's comment points to the problem. Date.parse can't know every possible combination of ways people might want to structure a date/datetime value. That's why Date supports strptime, which lets YOU define the pattern:
require 'date'
DateTime.strptime("05-31 13:30", '%m-%d %H:%M')
=> #<DateTime: 2013-05-31T13:30:00+00:00 ((2456444j,48600s,0n),+0s,2299161j)>
DateTime.strptime("5 31 13:30", '%m %d %H:%M')
=> #<DateTime: 2013-05-31T13:30:00+00:00 ((2456444j,48600s,0n),+0s,2299161j)>
The problem then becomes one of which format string to use for a given date string. In this test I changed the second datetime string so it's more obvious that the code is working correctly:
require 'date'
DATE_PATTERNS = {
/[0-2]\d-\d{2} \d{2}:\d{2}/ => '%m-%d %H:%M',
/[0-2]?\d \d{1,2} \d{2}:\d{2}/ => '%m %d %H:%M'
}
puts ["05-31 13:30", "5 31 13:31"].map { |str|
pattern = DATE_PATTERNS.keys.find { |k|
str[k]
}
puts pattern.source
DateTime.strptime(str[pattern], DATE_PATTERNS[pattern]).to_s
}
Which outputs:
[0-2]\d-\d{2} \d{2}:\d{2}
[0-2]?\d \d{1,2} \d{2}:\d{2}
2013-05-31T13:30:00+00:00
2013-05-31T13:31:00+00:00
In your time-string the year is missing.
parse tries to fix this but fails.
With the year given - like so
Time.parse("2013-05-31 13:30")
parse works.

Ruby DateTime.Parse to local time

I have a date string 20101129220021, so I will use
require 'date'
d = DateTime.parse('20101129220021')
This part works fine, and I get a date, which is in UTC.
My question is, how can I convert this into my local time? I tried many methods like extracting the time part using d.to_time and manipulate the result, but it didn't work. As far as I know, DateTime object is immutable. Can I please get some help?
irb(main):001:0> require "date"
=> true
irb(main):002:0> d = DateTime.parse('20101129220021')
=> #<DateTime: 2010-11-29T22:00:21+00:00 (70719276007/28800,0/1,2299161)>
irb(main):003:0> d.to_time
=> 2010-11-30 00:00:21 +0200
ruby 1.9.2p180 (2011-02-18)
You can add a rational fraction based on the timezone to get the local time.
require 'date'
# Make this whatever your zone is. Using UTC +0300 here.
ZONE = 3
d = DateTime.parse('20101129220021') + Rational(ZONE,24)
d.to_s # => "2010-11-30T01:00:21+00:00"

Resources