0

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.

Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101

1 Answers1

3

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
pje
  • 21,801
  • 10
  • 54
  • 70