March 12 - 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.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

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.

Test Cases

Code File