Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 731
AntonB Avatar asked Nov 19 '25 03:11

AntonB


1 Answers

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
like image 134
AntonB Avatar answered Nov 21 '25 19:11

AntonB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!