-5

I have a simple string date as: "11/28/2022"

And I try to convert to DateTime as:

 var currentStartDate = DateTime.ParseExact(
                model.StartDate,
                "dd/MM/yyyy",
                CultureInfo.InvariantCulture
            );

but it throw:

System.FormatException: The DateTime represented by the string '11/28/2022' is not supported in calendar 'System.Globalization.GregorianCalendar'.

Or

var currentStartDate = DateTime.Parse(model.StartDate);

But it is throwing an error two, how can I convert the date to DateTime?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Demian
  • 65
  • 5

1 Answers1

1

problem is that you are using day in place of the month and vice versa. so you have to correct it.

 var currentStartDate = DateTime.ParseExact(
                model.StartDate,
                "MM/dd/yyyy",
                CultureInfo.InvariantCulture
            );

And for your error, there is no month as such which is 28.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197