String was not recognized as valid date Time in C#

C#
DateInput is of DateTime type.
ExpirationDate comes from an out arguments that contain extracted dates from different websites.

So If I assign DateInput=DateTime.Parse(ExpirationDate)- It throws an error that the string was not recognized as valid datetime
Tried using Convert.todatetime(Expirationdate) and got the same error

Error I get is String was not recognized as valid date Time
(I want to check if DateInput<Datetime.Now, so I need DateInput in datetme format only)

1 Like

the essential demonstrated concepts can be adapted to your case and can also be used for C# when the statements are modified:

Show us the string value. Parse uses your system default datetime format to try to parse it. So if the string is in a different format you have to use ParseExact and specify the format.

Hey @Juhi_Patil

We need to just check if there is a desired format for your input date.

If yes, we can do the DateTime.ParseExact with date string and format of it.

Hope this helps

Thanks
#nK

@Juhi_Patil Try this

DateTime Output = DateTime.ParseExact(ExpirationDate, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

Let us know if you face any issues