-1

Looking for assistance in converting a date string i receive from a web form, where the format will be something like "10-April-2020". I need to save this into the database in the US date format "yyyy-mm-dd" so that the example date provided would go in as '2020-04-10'.

This is what I have so far, which complains that it is not a valid datetime.

    string LicenseExpiry = LicenseExpiry.Text;
    IFormatProvider culture = new CultureInfo("en-US", true);
    DateTime dateExpires = DateTime.ParseExact(LicenseExpiry, "yyyy-MM-dd", culture);

I have also tried the following which also fails.

DateTime dateExpires;
string LicenseExpiry = LicenseExpiry.Text;
IFormatProvider culture = new CultureInfo("en-US", true);
if (DateTime.TryParseExact(LicenseExpiry, "yyyy-MM-dd", culture, DateTimeStyles.None, out dateExpires))
{ 
    // Do something
}

Can anyone help with either of the attempts to see what went wrong? I am not allowed to change the Ui/Form to do any client side date manipulation either, and so my solution needs to be done in the C# code behind file.

Julian
  • 33,915
  • 22
  • 119
  • 174
Stephen85
  • 250
  • 1
  • 15
  • You data is a string in the form of `10-April-2020` which is nothing like the format you specify of `"yyyy-MM-dd"`. `ParseExact` means you will be specifying the exact format that the data is in. [Standard date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#:~:text=For%20example%2C%20the%20%22d%22,yyyy%2FMM%2Fdd%22.) – Ňɏssa Pøngjǣrdenlarp Aug 26 '20 at 02:26

1 Answers1

2

MM means the month number (from 01 through 12)

To parse 10-April-2020, you need MMMM, see Custom date and time format strings

The "MMMM" custom format specifier represents the full name of the month

Julian
  • 33,915
  • 22
  • 119
  • 174