Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert MM/dd/YYYY string date to datetime

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading