convert datetime to date format dd/mm/yyyy

后端 未结 13 2035
你的背包
你的背包 2020-11-27 18:14

I have a DateTime object 2/19/2011 12:00:00 AM. I want to convert this object to a string 19/2/2011.

Please help me to convert DateTime to

相关标签:
13条回答
  • 2020-11-27 18:50

    Here is a method, that takes datetime(format:01-01-2012 12:00:00) and returns string(format: 01-01-2012)

    public static string GetDateFromDateTime(DateTime datevalue){
        return datevalue.ToShortDateString(); 
    }
    
    0 讨论(0)
  • 2020-11-27 18:51
    DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
    
    string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-11-27 18:53

    As everyone else said, but remember CultureInfo.InvariantCulture!

    string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture)
    

    OR escape the '/'.

    0 讨论(0)
  • 2020-11-27 18:53

    this is you need and all people

       string date  = textBox1.Text;
    
            DateTime date2 = Convert.ToDateTime(date);
            var date3 = date2.Date;
            var D = date3.Day;
          var M =  date3.Month;         
          var y = date3.Year;
          string monthStr = M.ToString("00");
          string date4 = D.ToString() + "/" + monthStr.ToString() + "/" + y.ToString();
    
    
          textBox1.Text = date4;
    
    0 讨论(0)
  • 2020-11-27 18:57
    string currentdatetime = DateTime.Now.ToString("dd'/'MM'/'yyyy");
    
    0 讨论(0)
  • 2020-11-27 19:02

    First of all, you don't convert a DateTime object to some format, you display it in some format.

    Given an instance of a DateTime object, you can get a formatted string in that way like this:

    DateTime date = new DateTime(2011, 2, 19);
    string formatted = date.ToString("dd/M/yyyy");
    
    0 讨论(0)
提交回复
热议问题