简体   繁体   中英

Convert.ToDateTime() error

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);

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 :

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 .

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) , that is based on current culture . And Convert.DateTime without FormatProvider will use current culture too (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

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 . It return a true if it successfully converts the value, else it returns a false .

Please try with this.

CultureInfo objcul = new CultureInfo("en-GB");

DateTime.ParseExact(ValidFrom.Text,"dd/MM/yyyy", objcul);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM