Ruby function to format date

I need assistance with formatting a date string. I have a JSON object which contains the elements “start_date” and “end_date”. These elements contain date information in a string such as so:


I have created this method to format my start_date and end_date:

def format_date(date)
  date.to_time.strftime('%b %d')

What this method does is to format the date into this format of:

"Jul 15" 

This method helps me print the start_date and end_date into the form:

"Jul 15 to Jul 27" 

What I want, is to have my dates formatted in the form of:

"15 - 27 July 2015" #If the two dates fall within the same month

"15 July - 27 Aug 2015" #If the two dates fall into separate months

Could anyone assist me in writing such a ruby method?

Source: ruby

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.