0

I having a string strBuzday whose value is "09-19-2018".

string strBuzday = "09-19-2018";---Line 1

The next line is:

DateTime dt = Convert.ToDateTime(strBuzday);---Line 2

The above code does not work and

"String was not recognized as a valid DateTime" (Format exception is being thrown)

in Line 2.

I have two questions:

1.How to solve this?

2.What could be the reason as the same code works for every other developer in my team with every configuration in Visual studio being exactly the same,it does not work only for me.Why?

Edit: I ran the code in my machine and my co-workers machine and the result was same --9/20/2018 10:09:09 PM -- on the both the systems. One only difference between all other co-workers and my system is they are all on Windows 7 or 8 and I am the only one on Windows 10. So, I am assuming this may be the reason as to why I am the only one who's getting this.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Vikesh Gupta
  • 61
  • 1
  • 9
  • 1
    Possible duplicate of [String was not recognized as a valid DateTime " format dd/MM/yyyy"](https://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy) – mjwills Sep 20 '18 at 14:12
  • 1
    Use `DateTime.TryParse()` instead, that way you check a bool instead of getting an exception – maccettura Sep 20 '18 at 14:14
  • `DateTime dt = DateTime.Parse(strBuzday, "M-d-yyyy", CultureInfo.InvariantCulture);` – Dmitry Bychenko Sep 20 '18 at 14:18
  • To follow along from what @mjwills is talking about, there are _many_ acceptable date formats. I'm a Canadian (for whom 02/04/2018 would be April 2nd) but I live in the US, where everyone believes that that date is Feb 4th. There are also differences in date separators (slashes, dots, dashes). Your specific date is easy to parse in your head (there are only 12 months), but different machine settings could parse April 2nd/Feb 4th in different ways. Sending us the output of `DateTime.Now.ToString()` will allow us to get an idea about your settings – Flydog57 Sep 20 '18 at 14:35
  • @mjwills I ran the code in my machine and my co-workers machine and the result was same --9/20/2018 10:09:09 PM-- on the both the systems. One only difference between all other co-workers and my system is they are all on windows 7 or 8 and i am the only one on Windows 10.So,i am assuming this may be the reason as to why i am the only one who's getting this. – Vikesh Gupta Sep 20 '18 at 16:48

3 Answers3

3

If we have a look at Convert.ToDateTime code

https://referencesource.microsoft.com/#mscorlib/system/convert.cs,fce604498e2f810b

we'll find out

    public static DateTime ToDateTime(String value) {
        if (value == null)
            return new DateTime(0);
        return DateTime.Parse(value, CultureInfo.CurrentCulture);
    }

So it's CultureInfo.CurrentCulture which dictates format. To detect how Convert.ToDateTime will work, inspect CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern. If you have something like

   d.M.yyyy

then you are going to a have FormatException thrown on converting "09-19-2018" value: month can't be 19. You can either set the right CurrentCulture e.g.

  // en-US (USA) with right Date format     
  CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); 

  string strBuzday = "09-19-2018";
  // 19 Sep 2018 
  DateTime dt = Convert.ToDateTime(strBuzday);

Or modify current culture

  CultureInfo myCulture = CultureInfo.CurrentCulture.Clone() as CultureInfo;

  myCulture.DateTimeFormat.ShortDatePattern = @"M-d-yyyy";

  CultureInfo.CurrentCulture = myCulture;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1.How to inspect CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern ?can u tell through code? 2.CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); is resulting in error as CurrentCulture property is read only. – Vikesh Gupta Sep 20 '18 at 16:30
  • 1
    @Vikesh Gupta:It seems that you are using some *older* version of C#. 1. `System.Diagnostics.Debug.Write(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);` then see output window. 2. `System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");` for older C# version – Dmitry Bychenko Sep 20 '18 at 19:51
0

I suspect that it could be a formatting issue that's stopping you. I checked it out and it doesn't throw an error for me either which is weird. If this line of code works then it is most certainly your formatting.

this.Text = "09-19-2018";
DateTime dt = DateTime.ParseExact(this.Text, "MM-dd-yyyy", null);
  • 2
    Please read the comments on the question and Dmitry's answer. This is a **Culture** issue. That's why it works for some and not for others. – JuanR Sep 20 '18 at 14:47
0

From what you say (Format exception), it will have to be converted once defined as DateTime as shown below:

DateTime dt = DateTime.ParseExact("09-19-2018", "MM-dd-yyyy", CultureInfo.InvariantCulture);

string strBuzDay = dt.ToString = ("dd-MM-yyyy", cultureInfo.InvariantCulture);

If Parse.Exact does not work, then just DateTime should do.

foyss
  • 973
  • 2
  • 8
  • 24