Convert Date from 6/05/2020 format to dd/MM/YYYY format

后端 未结 5 1152
面向向阳花
面向向阳花 2020-12-03 18:24

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 da

5条回答
  •  有刺的猬
    2020-12-03 19:24

    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.

提交回复
热议问题