How to Convert date into MM/DD/YY format in C#

后端 未结 5 1310
自闭症患者
自闭症患者 2020-12-05 07:19

In My Asp.net webpage I need to display today\'s date into one of the textbox , so in my form load I wrote the following code

textbox1.text = System.DateTi         


        
相关标签:
5条回答
  • 2020-12-05 07:24

    See, here you can get only date by passing a format string. You can get a different date format as per your requirement as given below for current date:

    DateTime.Now.ToString("M/d/yyyy");
    

    Result : "9/1/2016"

    DateTime.Now.ToString("M-d-yyyy");
    

    Result : "9-1-2016"

    DateTime.Now.ToString("yyyy-MM-dd");
    

    Result : "2016-09-01"

    DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
    

    Result : "2016-09-01 09:20:10"

    For more details take a look at MSDN reference for Custom Date and Time Format Strings

    0 讨论(0)
  • 2020-12-05 07:27

    Have you tried the following?:

    textbox1.text = System.DateTime.Today.ToString("MM/dd/yy");
    

    Be aware that 2 digit years could be bad in the future...

    0 讨论(0)
  • 2020-12-05 07:33

    Look into using the ToString() method with a specified format.

    0 讨论(0)
  • 2020-12-05 07:34

    DateTime.Today.ToString("MM/dd/yy")

    Look at the docs for custom date and time format strings for more info.

    (Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)

    Others cultures really are a problem. For example, that code in portugues returns someting like 01-01-01 instead of 01/01/01. I also don't undestand why...

    To resolve that problem i do someting like this:

            IFormatProvider yyyymmddFormat = new System.Globalization.CultureInfo(String.Empty, false);
            return date.ToString("MM/dd/yy", yyyymmddFormat);
    
    0 讨论(0)
  • 2020-12-05 07:36
     DateTime.Today.ToString("MM/dd/yy")
    

    Look at the docs for custom date and time format strings for more info.

    (Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)

    0 讨论(0)
提交回复
热议问题