Parsing Date from Text Using Ruby

Parsing date from text using Ruby

Assuming you just want dates and not datetimes:

require 'date'
string = "Applications started after 12:00 A.M. Midnight (EST) February 1, 2010 will not be considered."
r = /(January|February|March|April|May|June|July|August|September|October|November|December) (\d+{1,2}), (\d{4})/
if string[r]
date =Date.parse(string[r])
puts date
end

[ruby]Get file, parse text and create date object

Something like this

holidays = File.read('holidays.txt').split(/\n/).map do |row| 
date, holiday_name = row.split(';')
date = Date.parse(date, '%d.%m.%Y')
[date, holiday_name]
end.to_h
=> {
#<Date: 2017-01-01 ((2457755j,0s,0n),+0s,2299161j)> => "New Year",
#<Date: 2017-04-16 ((2457860j,0s,0n),+0s,2299161j)> => "Easter",
#<Date: 2017-12-25 ((2458113j,0s,0n),+0s,2299161j)> => "Christmas"
}

Ruby String to Date Conversion

What is wrong with Date.parse method?

str = "Tue, 10 Aug 2010 01:20:19 -0400 (EDT)"
date = Date.parse str
=> #<Date: 4910837/2,0,2299161>
puts date
2010-08-10

It seems to work.

The only problem here is time zone. If you want date in UTC time zone, then it is better to use Time object, suppose we have string:

str = "Tue, 10 Aug 2010 01:20:19 +0400"
puts Date.parse str
2010-08-10
puts Date.parse(Time.parse(str).utc.to_s)
2010-08-09

I couldn't find simpler method to convert Time to Date.

How do I find and extract a date from string values

How about this?

s = "some string value with a date 02/02/2002"
regex = /\d{1,2}\/\d{1,2}\/\d{4}/

s[regex] # => "02/02/2002"

Note that you don't need the ^ and $ in your regex as you won't be matching the string in its entirety.

Parsing a string to a date in Ruby

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"

How do I extract all dates from a text?

Use a regex to scan for \d{2}/\d{2}/\d{4} and then collect the results into an array or simply print them as they're matched.

File.open("your_file.txt").read().scan(/\d{2}\/\d{2}\/\d{4}/)

RubyFiddle.

Converting string to date with Ruby

Remove the period from my_string and from the date pattern.

Date.strptime(my_string.sub('.', ''), '%d %b %Y')

That's assuming you have at most one dot in my_string. If there may be several, use gsub.

Date.strptime(my_string.gsub('.', ''), '%d %b %Y')

Extract Date from string ruby

str = "Rep. Barletta, Lou [R-PA-11] (Introduced 06/04/2015)"
str.match(/(\d{2}\/\d{2}\/\d{4})/)[0]
#=> "06/04/2015"

This code matches anything that's in the format of 2 numbers/2 numbers/4 numbers and returns it.

If there's a possibility of having XX/XX/XXXX somewhere else in the string, I'd probably use the following code instead:

str = "Rep. Barletta, Lou [R-PA-11] (Introduced 06/04/2015)"
str.match(\(Introduced (\d{2}\/\d{2}\/\d{4})\)$)[0]
#=> "06/04/2015"

This searches for (Introduced XX/XX/XXXX) and grabs the date from that in particular.



Related Topics



Leave a reply



Submit