2

Good day All,

Before this, I have a c# 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.

Panadol Chong
  • 1,793
  • 13
  • 54
  • 119
  • There are **a lot** of DateTime formatting questions on SO. – CodeCaster Oct 29 '13 at 11:29
  • possible duplicate of [String was not recognized as a valid DateTime?](http://stackoverflow.com/questions/10123145/string-was-not-recognized-as-a-valid-datetime) – CodeCaster Oct 29 '13 at 11:29
  • 1
    **Why** Are you converting a DateTime to a string, and then back again immediately? Why not simply `DateTime dt = allowedDateTime;`? – Lasse V. Karlsen Oct 29 '13 at 11:31

4 Answers4

5

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

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • Your answer have point : ) Would like to ask, where/how to changes my machines date cultures? Just ask for testing purpose. – Panadol Chong Oct 29 '13 at 11:54
  • @PanadolChong you can change your culture in code: http://stackoverflow.com/questions/7000509/how-to-change-currentculture-at-runtime – Kamil Budziewski Oct 29 '13 at 12:05
3

use DateTime.ParseExact()

DateTime dt = DateTime.ParseExact(formatted, "M/dd/yyyy", null);
John Woo
  • 258,903
  • 69
  • 498
  • 492
2

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Your answer have point : ) Would like to ask, where/how to changes my machines date cultures? Just ask for testing purpose. – Panadol Chong Oct 29 '13 at 11:59
  • @PanadolChong You can change it on _regional settings_ part in control panel. Remember for upvoting and [accepting as an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) one of these answers.. – Soner Gönül Oct 29 '13 at 12:02
0
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 .

Sunil Acharya
  • 1,153
  • 5
  • 22
  • 39