Natural Language Date Parser for Ruby/Rails

Natural language date parser for ruby/rails

Do not forget that everything being an object in Ruby, you have already some pretty readable statement from the language itself:

2.weeks.from_now.utc

would be the equivalent of "two weeks from today".

However, for real natural language, may be chronic might be a more specialized library at rubyforge.

Chronic.parse('this tuesday 5:00')
#=> Tue Aug 29 17:00:00 PDT 2006

Ruby/Rails - Convert Date/Time back into Natural Language (2011-02-17 = February 17th, 2011)

Take a look at strftime, this is a link to it's implementation for the Time class.

With it you should be able to get the date to do anything you want :-)

>> Time.now.strftime("%A, %B %d, %Y at %l%p")
=> "Thursday, February 17, 2011 at 5PM"

Natural language dates in ruby/rails?

I think what you are looking for is time_ago_in_words

Ruby/Rails - Convert Date/Time back into Natural Language (2011-02-17 = February 17th, 2011)

Take a look at strftime, this is a link to it's implementation for the Time class.

With it you should be able to get the date to do anything you want :-)

>> Time.now.strftime("%A, %B %d, %Y at %l%p")
=> "Thursday, February 17, 2011 at 5PM"

Ruby/Rails: Converting datetime to natural language (e.g. 3/23/2012 to this Friday )

This is a little hack, totally unfinished and inelegant. But Ruby/Rails is so awesome with dates, and ranges are so perfect, maybe if you don't find the gem, something like this will get you started:

module HumanDate
def date_difference_for_people (from, to)
seconds_in_day = 24 * 60 * 60
days_difference = ((to - from)/seconds_in_day).round
case days_difference
when -1
"yesterday"
when 0
"today"
when 1
"tomorrow"
when 2..days_left_in_week
"this #{day_name(difference)}"
when (days_left_in_week + 1)..(days_left_in_week + 7)
"next week"
when (days_left_in_week + 8)..days_left_in_month
"later this month"
else
"later -- how much left to you :-)"
end
end

def day_name(days_from_now)
days_from_now.days_from_now.strftime("%A")
end

def days_left_in_month
Time.now.end_of_month.day - Time.now.day
end

def days_left_in_week
Time.now.end_of_week.day - Time.now.day
end
end

Natural language date/time parser for .NET?

We developed exactly what you are looking for on an internal project. We are thinking of making this public if there is sufficient need for it. Take a look at this blog for more details: http://precisionsoftwaredesign.com/blog.php.

Feel free to contact me if you are interested: contact@precisionsoftware.us

This library is now a SourceForge project. The page is at:

http://www.SourceForge.net/p/naturaldate

The assembly is in the downloads section, and the source is available with Mercurial.



Related Topics



Leave a reply



Submit