- Read Tutorial
- Watch Guide Video
- Complete the Exercise
Summary
Convert a string that contains a date and a specific format into a Ruby date object.
Exercise Description
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).
Examples
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.