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

牧云@^-^@ 提交于 2019-11-26 18:29:03

问题


I am facing a small issue which i am not able after trying so many things so here it goes ..... There is a text box in my page in which i am entering date and i want that date in a datetime object.

for ex : date entered : 11/2/2010 (dd/MM/yyyy) should be in same format when i am accessing it in date time object but it is getting changed to (2/11/2011 ie: MM/dd/yyyy format).

i hope i am making sense here all i want is some thing like this.....

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

dt should be (11/2/2010 rather then 2/11/2010)

@oded after using the following code

DateTime sDate, eDate = new DateTime(); 

//To modify 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 am getting in edate and sdate is 1/1/0001 12:00:00 AM where it should be 3/11/2011.


回答1:


EDIT: This value: "11/2/2010" doesn't match the format "dd/MM/yyyy". It matches the format "d/M/yyyy" - for "dd/MM/yyyy" it should be "11/02/2010".

That's why TryParseExact is failing for you. You need to pick the right format pattern.


A DateTime value doesn't have a format. It just represents date and time (in the ISO calendar, and possibly in different time zones, but that's a different matter). It's like an int - it doesn't represent "a decimal integer" or "a hex integer" - it's just an integer within a particular range. You can format a number as decimal or hex, but it doesn't inherently have a format.

It sounds like you should parse it with ParseExact to specify the format when converting from the textbox, or probably TryParseExact:

// This is assuming you're absolutely sure of the format used. This is *not*
// necessarily the user's 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 should keep that value as DateTime for all purposes except giving it back to a user - at which point you may well want to use their cultural settings.

In particular, if you're storing the value in a database you should not convert it to text and include it in a SQL statement - that's asking for trouble. Instead, use a parameterized SQL statement and set it as the parameter value, still as a DateTime.




回答2:


DateTime doesn't store dates in any specific format - it uses an internal representation (what exactly shouldn't matter).

After parsing the string to a DateTime, there is no inherent format there. There is only a format when you output the value. What you see in the debugger is simply a conversion to a string using your system settings.

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

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

The converse also applies - if you need to parse the string unambiguously, use ParseExact or TryParseExact (both static members of of DateTime):

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 about custom and standard Date and Time format strings.




回答3:


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

As this thread and this article pointed out.




回答4:


Try DateTime.Parse with an appropriate format provider. In your case it should be

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



回答5:


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

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



来源:https://stackoverflow.com/questions/7674355/convert-date-from-mm-dd-yyyy-format-to-dd-mm-yyyy-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!