Reputation: 5411
I have a date coming in this format "MM-DD-YYY" from view and i want to save it in datetime format.
I'm trying to convert to datetime format using strftime
but it is recognizing the date in "DD-MM-YYYY" format and therefore whenever my date exceeds 12 my code breaks.Please help.
After debugging i found another issue,the date is not coming when the date exceeds 12,which means if the date is not in "DD-MM-YYYY"fomat, it is not shown in the controller.
Upvotes: 0
Views: 808
Reputation: 22707
You can parse a string with a specified formatting using DateTime.strptime:
str = '12-31-1999' # intended to mean "December 31st, 1999"
parsed = DateTime.strptime(str, '%m-%d-%Y')
parsed.month
# => 12
Upvotes: 3