0

Why does this:

Convert.ToDateTime("08/31/2017")

throw an System.FormatException but not this:

Convert.ToDateTime("09/12/2017")

If you need more information please ask and I will update or comment. I have no clue what is causing this issue, so I don't know what details you need.

Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
Jc Balantakbo
  • 115
  • 1
  • 8
  • 1
    Probably because it is interpreting the date as day first and then month. You need to use DateTime.ParseExact – Igor Nov 08 '17 at 21:52
  • Because American way of writing dates is invalid unless you specify custom formatting. – Arijoon Nov 08 '17 at 21:52
  • Most likely because the second is being interpreted as day 9 of month 12 ( December 12, 2017, a valid date) while the first is being interpreted as day 8 of month 31, 2017 and there is no 31st month. – Ken White Nov 08 '17 at 21:53

1 Answers1

2

The default order for this date format on your computers culture is Day/Month/Year, as 31 is not a valid month it fails. If you want this order, you need to provide the format with it:

var x = DateTime.ParseExact("08/31/2017", "MM/dd/yyyy",CultureInfo.InvariantCulture);
Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49