-5

I have the following code in my project (which was taken from https://stackoverflow.com/a/12257557).

string r = "08/05/2015";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime t1 = DateTime.ParseExact(r, "yyyy-MM-dd", culture);

the value inside r is in MM/dd/yyyy

For the ParseExact I am getting the error

string was not recognized as a valid date time.

I gone through most of the question in stackoverflow all are giving the above codes. Do I am missing anything?

Community
  • 1
  • 1
Sachu
  • 7,555
  • 7
  • 55
  • 94
  • Follow this, it may help you. http://stackoverflow.com/questions/31823320/how-to-convert-string-value-jul-28-0000-to-datetime-in-c-sharp/31823421#31823421 – NASSER Aug 05 '15 at 05:33
  • 1
    Funny how a date that's in `MM/dd/yyyy` is deemed invalid when asking it to be parsed as `yyyy-MM-dd`. – Brendan Green Aug 05 '15 at 05:34
  • What part of `DateTime.ParseExact` don't you understand? –  Aug 05 '15 at 05:35

3 Answers3

2
DateTime t1 = DateTime.ParseExact(r, "MM/dd/yyyy", culture);
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24
1

replace

DateTime t1 = DateTime.ParseExact(r, "yyyy-MM-dd", culture);

with

DateTime t1 = DateTime.ParseExact(r, "MM/dd/yyyy", CultureInfo.InvariantCulture);
fubo
  • 44,811
  • 17
  • 103
  • 137
1

Parse exact required exact format. So format must be MM/dd/yyyy.

string r = "08/05/2015";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime t1 = DateTime.ParseExact(r, "MM/dd/yyyy", culture);

for details check this

Rohit Harkhani
  • 656
  • 4
  • 9