javarubydatefunctional-programming

Ruby string to date time format


I have multiple dates coming in from user input, examples:

MM-DD-YYYY

6-3-1975

MM/DD/YYYY

4/23/1967

using the ruby built in require 'time'

and trying to Time.parse("4/23/1967")

gives me the error time.rb : argument out of range.

Any solutions to convert different user inputs with either dashes or slashes to parse?


Solution

  • Solution:

    require 'date'
    
    userDate = "4-23-1967"
    readDate = Date.strptime(userDate, "%m-%d-%Y")
    convertedDate = readDate.strftime("%-m/%-d/%Y")
    puts convertedDate
    
    OUTPUT: 4/23/1967