convert datetime to date format dd/mm/yyyy

C#.Net

C# Problem Overview


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 string format.

C# Solutions


Solution 1 - C#

DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

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

Solution 2 - C#

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");

Solution 3 - C#

As everyone else said, but remember CultureInfo.InvariantCulture!

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

OR escape the '/'.

Solution 4 - C#

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('-','/');

Solution 5 - C#

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

DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)

Solution 6 - C#

It's simple--tostring() accepts a parameter with this format...

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

Solution 7 - C#

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(); 
}

Solution 8 - C#

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");

Solution 9 - C#

If you want the string use -

DateTime.ToString("dd/MM/yyyy")

Solution 10 - C#

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");
    }

Solution 11 - C#

string currentdatetime = DateTime.Now.ToString("dd'/'MM'/'yyyy");

Solution 12 - C#

In C# 10 you can use DateOnly.

DateOnly date = new(2011, 02, 19);
string output = date.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

Solution 13 - C#

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);

Solution 14 - C#

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAswathiView Question on Stackoverflow
Solution 1 - C#KarelView Answer on Stackoverflow
Solution 2 - C#Jeff MercadoView Answer on Stackoverflow
Solution 3 - C#xanatosView Answer on Stackoverflow
Solution 4 - C#Siddhesh AdarkarView Answer on Stackoverflow
Solution 5 - C#HemanthView Answer on Stackoverflow
Solution 6 - C#smruti swarupView Answer on Stackoverflow
Solution 7 - C#awaispkView Answer on Stackoverflow
Solution 8 - C#SmurView Answer on Stackoverflow
Solution 9 - C#Jason QuinnView Answer on Stackoverflow
Solution 10 - C#Anderson MejíaView Answer on Stackoverflow
Solution 11 - C#Hemant yadavView Answer on Stackoverflow
Solution 12 - C#Misha ZaslavskyView Answer on Stackoverflow
Solution 13 - C#MaheshView Answer on Stackoverflow
Solution 14 - C#GLOBAL TECHView Answer on Stackoverflow