Convert date from MM / dd / YYYY format to dd / MM / YYYY format

I ran into a small problem that I am not capable of after trying so many things, so here it goes ..... On my page on which I find the date there is a text box and I want this date in the date object.

for ex: date entered: 11/2/2010 (dd / MM / yyyy) should be in the same format when I access it in an object with a date, but it changes to (2/11/2011, i.e. format MM / dd / yyyy).

Hope I make sense here; all I want is something like this .....

DateTime dt = convert.ToDateTime(txtDate.Text); 

dt should be (11/2/2010, not 11/2/2010)

@oded after using the following code

 DateTime sDate, eDate = new DateTime(); 

// To change the dates for our use. DateTime.TryParseExact (txtFrom.Text, "dd / MM / yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out sDate);

 DateTime.TryParseExact(txtFrom.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out eDate); 

What I get in edate and sdate is 1/1/0001 12:00:00 where it should be 3/11/2011.

+8
c # datetime datetime-format
source share
5 answers

EDIT: This value: "11/2/2010" does not match the format "dd / MM / yyyy". It corresponds to the format "d / M / yyyy" - for "dd / MM / yyyy" it should be "11/02/2010".

This is why TryParseExact fails for you. You need to choose the correct format.


A value of DateTime has no format. It simply represents the date and time (in the ISO calendar and possibly in different time zones, but that's another matter). It is like an int - it does not represent a "decimal integer" or "hexadecimal integer" - it is just an integer in a certain range. You can format the number as decimal or hexadecimal, but it essentially has no format.

It looks like you should ParseExact it with ParseExact to indicate the format when converting from a text field or, possibly, TryParseExact :

 // This is assuming you're absolutely sure of the format used. This is *not* // necessarily the user preferred format. You should think about where your // data is coming from. DateTime date; if (DateTime.TryParseExact(text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { // Okay, successful parse. We now have the date. Use it, avoiding formatting // it back to a string for as long as possible. } 

You must save this value as a DateTime for all purposes except returning it to the user - at this point you may want to use your cultural settings.

In particular, if you store a value in a database, you should not convert it to text and include it in an SQL query that requires trouble. Instead, use a parameterized SQL statement and set it as a parameter value, still as a DateTime .

+11
source share

DateTime does not store dates in any particular format - it uses an internal representation (which certainly does not matter).

After parsing the string before DateTime there is no inline format there. When issuing a value, only the format exists. What you see in the debugger is just converting to a string using system settings.

If you want to format DateTime , use ToString with a format string:

 dt.ToString("dd/MM/yyyy"); 

The opposite also applies - if you need to parse a string uniquely, use ParseExact or TryParseExact (both static DateTime members):

 DateTime dt; if(DateTime.TryParseExact(txtDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out td)) { // Valid date used in `txtDate.Text`, use dt now. } 

Read custom and standard string date and time format.

+11
source share

To avoid any error in the months / days when parsing a date, it is probably better to use DateTime.Parse or DateTime.ParseExact than ToDateTime .

Like this thread and in this article .

+2
source share

Try using DateTime.Parse with the appropriate format provider. In your case, it should be

 IFormatProvider culture = new CultureInfo("de-DE", true); DateTime.Parse(txtDate.Text, culture ); 
-one
source share

If you want to access it in a specific format, you should use DateTime.ToString (string format).

http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx

-one
source share

All Articles