I'm aware of Chronic, the Natural Language Parser for converting language into database calculating format, but I'm wondering how I can convert that data back into something humans can understand easily.
For example:
Chronic.parse('today') => 2011-02-17 17:30:00 -0500
So is there a way to take "2011-02-18 20:00:00 " and represent it as " Friday, February 18th, 2011 at 10pm "??
Essentially the reverse of Chronic?
For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.
Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.
DateTime is considered deprecated. It only still exists to be backwards-compatible. As of Ruby 3 (released December 24th, 2020, as is Ruby tradition), DateTime only exists for backwards-compatibility. The date library docs recommend you just use Time instead.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With