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.

Summary

Convert a string that contains a date and a specific format into a Ruby date object.

Exercise File

Code File

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.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on