Question: I get an error trying to convert a `string` to `DateTime`, even though this has always worked before. 

This is the procedure I used:

1. Save a datetime to a text file like this: 

```
DateTime.Now.ToUniversalTime().ToString(); //results in something like this 20.9.2015 10.16.12

```
2. On application load up:

```
string s = streamReader.ReadLine(); //the saved string s = "20.09.2015 10.16.12"
DateTime d = Convert.ToDateTime(s);

```

This results in this: 

> 
> String was not recognized as a valid DateTime.
> 
> 
> 

I have never experienced this problem before I installed Windows 10 and Visual Studio 2015, my previous setup was Windows 7 and Visual Studio 2013. The weird thing is that this also results in the same error:

```
DateTime d = Convert.ToDateTime(DateTime.Now.ToUniversalTime().ToString());

```

This did work perfectly in my previous setup, any ideas why it does not work any more?

Edit: I do believe that this question is not a duplicate of the question Converting a String to DateTime that Thomas Weller linked to. Because this problem is the result of changes in expected behaviour, see the second example. Also I did find a fix to this, but it is not practical:

```
    string s = DateTime.Now.ToUniversalTime().ToString(); 
    s = s.Substring(0, s.IndexOf(" ")).Replace('.', '/') + s.Substring(s.IndexOf(" ")).Replace('.', ':'); 
    DateTime d = Convert.ToDateTime(s);

```

Answer: 
This probably does not work anymore due to your regional settings on control panel.

To avoid conflicts with regional settings on target enviroment, use [DateTime.TryParseExact](https://msdn.microsoft.com/pt-br/library/h9b85w22(v=vs.110).aspx):

```
string s = streamReader.ReadLine(); //the saved string s = "20.09.2015 10.16.12"
DateTime d = DateTime.Now;
DateTime.TryParseExact(s, "dd.MM.yyyy HH.mm.ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);

```

Also, if this is your default format and you need this format for entire application, [you can set the default culture on your config file](https://stackoverflow.com/questions/9104084/how-do-i-set-cultureinfo-currentculture-from-an-app-config-file).

This code:

```
Convert.ToDateTime(DateTime.Now.ToUniversalTime().ToString())

```

Should work on any enviroment, once that DateTime.ToString() and Convert.ToDateTime() without a format provider uses the same DateTimeFormatInfo, unless you are changing your culture between these calls. Note that DateTime.ToString() without format specifier [will use General date/time pattern (G)](https://msdn.microsoft.com/pt-br/library/zdtaw1bw(v=vs.110).aspx), that is based on [current culture](https://msdn.microsoft.com/pt-br/library/az4se3k1(v=vs.110).aspx#GeneralDateLongTime). And Convert.DateTime without FormatProvider [will use current culture too](https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx) (check these references on MSDN).

My last suggestion is, instead of doing replaces, you can do:

```
string s = DateTime.Now.ToUniversalTime().ToString("dd/MM/yyyy HH:mm:ss");

```
I tried following code in console application, and it worked for me. And check .NETFiddle [here](https://dotnetfiddle.net/nxsz87)

```
string s = "20.09.2015 10.16.12";
DateTime d;
bool isValid = DateTime.TryParseExact(s, "dd.MM.yyyy HH.mm.ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);

```

Try to understand how TryParseExact works. You can read about `TryParseExact` and formats [here](https://msdn.microsoft.com/en-us/library/h9b85w22(v=vs.110).aspx). It return a `true` if it successfully converts the value, else it returns a `false`.