-2

My dateformat is dd/MM/yyyy.

I have a date column in my file with values like 1/08/2019 to 31/08/2019.

But I'm getting the following error when processing that file:

System.FormatException: String was not recognized as a valid DateTime.

at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ratha
  • 9,434
  • 17
  • 85
  • 163
  • 1
    Can you add code that you tried. – Sailesh Babu Doppalapudi Nov 01 '19 at 00:03
  • 1
    That first date (`1/08/2019`) wouldn't work with the format you specified, since it has a one-digit day. You can remove one of the **`d`** characters, or pass in an array of format strings where you account for double and single digit days (and months, too). – Rufus L Nov 01 '19 at 00:07
  • @RufusL , There is no possible to change the code at this time. Only option to change the dateformat which is passed via a config file. Could you please tell me which corrcet date format will work for all date ranges as I pointed ABOVE? – Ratha Nov 01 '19 at 00:11
  • 1
    Look at the first date you used as an example. It has a single digit day. Look at the format string. It specifies a double digit day. Changing the format to `"d/M/yyyy"` is your safest bet, since that will handle both single and double digit days and months. – Rufus L Nov 01 '19 at 00:14

2 Answers2

4

You've specified in your format string that the days and months must be double digits, but it appears that your input can be single digits.

In order to solve this, you need to specify a single digit in the format string by using a single d for the day portion (and a single M for the month, too).

It's also safe to use a single digit in the format string, since it will handle both single and double digits.

So your format string should look like: "d/M/yyyy"

For example, these all work:

var a = DateTime.ParseExact("1/8/2019", "d/M/yyyy", CultureInfo.CurrentCulture);
var b = DateTime.ParseExact("1/08/2019", "d/M/yyyy", CultureInfo.CurrentCulture);
var c = DateTime.ParseExact("01/8/2019", "d/M/yyyy", CultureInfo.CurrentCulture);
var d = DateTime.ParseExact("01/08/2019", "d/M/yyyy", CultureInfo.CurrentCulture);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1

difficult to say as you show the error but not the actual code, as you have dates in your file of differennt format, like d/MM/yyyy and dd/MM/yyyy try to use TryParse instead of ParseExact and if the TryParse fails with one format ( d/MM/yyyy ), then do another TryParse with the second format ( dd/MM/yyyy ) that way you should be able to cover both cases.

Again, without seeing the code it is difficult to give more detailed feedback.

also you could use an approach with TryParseExact and multiple format strings, like shown here:

var formatStrings = new string[] { "MM/dd/yyyy hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss" };

if (DateTime.TryParseExact(dt, formatStrings, enUS, DateTimeStyles.None, out dateValue))
return dateValue;

see this SO answer: https://stackoverflow.com/a/17859959/559144

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • MY CODE IS ObservationDateTime = DateTime.ParseExact(sira.ObservationDate, dataServiceConfig.SIRADataServiceFileDateFormat, CultureInfo.InvariantCulture). HereFormat is dd/MM/yyyy – Ratha Nov 01 '19 at 00:12