0

I have also used Parse.Exact with cultureInfo invariant culture nothing seems to be work. only when the value is like "27-Mar-2020" then only it is working please find me a solution.

birthdate = string.IsNullOrEmpty(DS.Tables[0].Rows[i][Values[j]].ToString()) ? null : (DateTime.Parse((DS.Tables[0].Rows[i][Values[j]]).ToString())).ToString("dd-MMM-yyyy hh:mm:ss");
BKM
  • 186
  • 2
  • 15
  • 1
    Show your `ParseExact()` attempt – Mathias R. Jessen Mar 27 '20 at 13:18
  • DateTime.ParseExact(DS.Tables[0].Rows[i][Values[j]].ToString(), "dd-MMM-yyyy hh:mm:ss", CultureInfo.InvariantCulture).ToString(); – BKM Mar 27 '20 at 13:21
  • 1
    What is the data type of `DS.Tables[0].Rows[i][Values[j]]`? (try `DS.Tables[0].Rows[i][Values[j]].GetType()`) – C.Evenhuis Mar 27 '20 at 13:28
  • Why would you include `hh:mm:ss` in the format string if the input is just `dd-MMM-yyyy`? – Mathias R. Jessen Mar 27 '20 at 13:29
  • Don't use Parse Exact. Use Parse which automatically accepts a wide range of formats. Only use Parse Exact when using a format that isn't accepted by Parse. Parse exact will accept an array of formats strings. – jdweng Mar 27 '20 at 13:31
  • @jdweng i have used parse only in the end please look at my question. – BKM Mar 27 '20 at 13:32
  • @BKM : Maybe you should read the posting again. – jdweng Mar 27 '20 at 13:34
  • @C.Evenhuis capital 'S'tring type – BKM Mar 27 '20 at 13:39
  • 1
    @BKM okay, usually ADO drivers support the `DateTime` format, was hoping I could save you the trouble of parsing something that was a datetime to begin with. So you'll have to support any format that someone has decided to put in that string, right? Including the string "yesterday" if they decide to do so. `ParseExact` can and _should_ be used when you **know** the format in which the string is stored, `Parse` *might* work when you don't know the format. – C.Evenhuis Mar 27 '20 at 13:42
  • No i am taking value from a csv file and there can be two formats "dd/MM/yyyy" or "dd-MMM-yyyy" but i had to convert it to dd-MMM-yyyy at the end anyway. @C.Evenhuis – BKM Mar 27 '20 at 13:44

1 Answers1

1

If you know the format in which the date strings appear in the CSV file, you should be able to parse them using ParseExact.

Be sure to specify the exact format. In the ParseExact example you commented, you're supplying "dd-MMM-yyyy hh:mm:ss" as a format, but that would never work on dates that do not include a timestamp.

Assuming the two possible formats you've mentioned, this should work:

DateTime.ParseExact(
    dateString, 
    new string[] { "dd/MM/yyyy", "dd-MMM-yyyy" },
    CultureInfo.InvariantCulture,
    DateTimeStyles.None);

PS: I prefer using TryParseExact by the way, to prevent an exception from being raised. That would look something like this:

    if (!DateTime.TryParseExact(
        dateString, 
        new string[] { "dd/MM/yyyy", "dd-MMM-yyyy" },
        CultureInfo.InvariantCulture,
        DateTimeStyles.None,
        out DateTime parsedDateTime))
    {
        // parsing has failed, you could now 
        //   - throw an exception
        //   - assign a default value to the date time field
        //   - ...
    }

    // parsedDateTime can be used here
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • the format i am providing to csv is 28/03/2020 05:45:00 and your solution is not working it is giving me "string was not recognizd a valid datetime." – BKM Mar 28 '20 at 04:42
  • hey thanks i tried after and it worked i got your point, if i know the exact format then only i have to use parse.exact... can you help me with tryparseExact as it is asking the parameter provider. – BKM Mar 28 '20 at 06:38
  • 1
    @BKM good to hear you've got it working, an example for TryParseExact added to the answer. – C.Evenhuis Mar 28 '20 at 07:21
  • thank you :) can you upvote my question ? – BKM Mar 28 '20 at 07:57