Parse a String Based Date and Convert it Into a Ruby Date Object
Working with dates in web applications can pose a unique challenge, especially when working with multiple date formats. This guide focuses on how to convert string based strings in a specific format into a Ruby date object so that you can utilize it in your own program.
Convert a string that contains a date and a specific format into a Ruby date object.
Given a string date with the format
mm/dd/yyyy, convert the string into a Ruby date object that can be treated like a date created directly in Ruby (meaning that you can perform tasks such as calling methods on it).
str_date = "07/31/2018" date_parser(str_date).month # => 7 date_parser(str_date).leap? # => false
Real World Usage
This is a common challenge in modern web applications. Imagine a scenario where you have a jQuery date picker in a Rails application. That date picker is going to send the date as a string in a specific format. Typically the format is one that can’t be properly interpreted by the Rails application. This means that you will need to convert the string and format into a Ruby date object so that it can be used properly.
Can be found on the solutions branch on github.