简体   繁体   中英

“String was not recognized as a valid DateTime.” Error occur in Window 7 computer environment

Good day All,

Before this, I have ac# system in a VM with Microsoft Window XP. I have some code to convert string to date time, the following is part of my code :

DateTime allowDateTime = DateTime.Now.AddMonths(-2);
string formatted = allowDateTime.ToString("M/dd/yyyy");
DateTime dt = Convert.ToDateTime(formatted);

if (redempDateConvert < dt)
   td.Text = "";

Until this point, everything is working fine. After that, I move my all source code without any changes, and data base and set it up in my real machine (Window 7).

System is working fine, I am still able to log in and control the system like usual.

Until today, I have reach to this part, and browser displayed error message : String was not recognized as a valid DateTime. in line 397.

Here I displayed my code again (with explanation):

    DateTime allowDateTime = DateTime.Now.AddMonths(-2);
    string formatted = allowDateTime.ToString("M/dd/yyyy");
    DateTime dt = Convert.ToDateTime(formatted);  //here is line 397, which is the error happening.

    if (redempDateConvert < dt)
       td.Text = "";

I have checked both (VM and my real machine) environment, both running in .Net 4.0.

Just curious on why the same code, but there is an error happen in my real machine. Is that I miss out to configure something? Kindly advise.

It's because / means default date separator, your machines have different cultures. If you are always getting / as date separator and have culture that accepts - as date separator it will fail.

use ParseExact to avoid errors with different culture:

DateTime dt = DateTime.ParseExact(formatted, "M/dd/yyyy", null);

code above will parse date with / as date separator no matter which culture you will be using

使用DateTime.ParseExact()

DateTime dt = DateTime.ParseExact(formatted, "M/dd/yyyy", null);

Here how Convert.ToDateTime method looks like when you decompile it;

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

As you can see, this method use DateTime.Parse method with your CurrentCulture . And if your string doesn't match your current culture date format, your code will be broken. That's the reason you get this error.

Just curious on why the same code, but there is an error happen in my real machine. Is that I miss out to configure something?

/ seperator has a special meaning of " replace me with the current culture's date separator "

Probably your virtual machine and real machine have different culture and that's why they have different date seperators .

DateTime dt = DateTime.Now; // get current date time
txtFormat.Text = string.Format("{0:yyyy/dd/MMM hh:mm:ss}", dt); // you can specify format according to your need

Format can be such as dd/mm/yy dd/mmm/yyyy mmm/dd/yy mm/dd/yy mm/dd/yyyy

Note : you can use any separator in format .

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