convert datetime to date format dd/mm/yyyy

后端 未结 13 2034
你的背包
你的背包 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:38

    If you want the string use -

    DateTime.ToString("dd/MM/yyyy")
    
    0 讨论(0)
  • 2020-11-27 18:42

    On my login form I am showing the current time on a label.

        public FrmLogin()
        {
            InitializeComponent();
            lblTime.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
        }
    
        private void tmrTime_Tick(object sender, EventArgs e)
        {
            lblHora.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
        }
    
    0 讨论(0)
  • 2020-11-27 18:45

    DateTime.ToString("dd/MM/yyyy") may give the date in dd-MM-yyyy format. This depends on your short date format. If short date format is not as per format, we have to replace character '-' with '/' as below:

     date = DateTime.Now.ToString("dd/MM/yyyy").Replace('-','/');
    
    0 讨论(0)
  • 2020-11-27 18:47

    You can use the ToString() method, if you want a string representation of your date, with the correct formatting.

    Like:

    DateTime date = new DateTime(2011, 02, 19);
    string strDate = date.ToString("dd/MM/yyyy");
    
    0 讨论(0)
  • 2020-11-27 18:47

    This works for me:

    string dateTimeString = "21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30";
    dateTimeString = Regex.Replace(dateTimeString, @"[^\u0000-\u007F]", string.Empty);
    
    string inputFormat = "dd-MM-yyyy HH:mm:ss";
    string outputFormat = "yyyy-MM-dd HH:mm:ss";
    var dateTime = DateTime.ParseExact(dateTimeString, inputFormat, CultureInfo.InvariantCulture);
    string output = dateTime.ToString(outputFormat);
    
    Console.WriteLine(output);
    
    0 讨论(0)
  • 2020-11-27 18:48

    You have to pass the CultureInfo to get the result with slash(/)

    DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
    
    0 讨论(0)
提交回复
热议问题