75

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.

Andrew
  • 18,680
  • 13
  • 103
  • 118
Aswathi
  • 973
  • 2
  • 7
  • 9
  • 1
    Is it `object` a `DateTime`? By convert, you mean you want a string formatted? How do you define the "the most possible answers"? – The Scrum Meister Feb 19 '11 at 10:02
  • 1
    Do you want string formatting, or do you want just the date? – Jean Hominal Feb 19 '11 at 10:04
  • 3
    Please make your question *much, much* clearer. Read http://tinyurl.com/so-hints – Jon Skeet Feb 19 '11 at 10:05
  • 5
    I'm really not a person to complain about laziness (I'm myself lazy way too often), but this is clearly a case of not looking before asking, just look at how many related questions exist, with even **the exact same date format string** (and I have no doubt that this is what aswathi is after) – ntziolis Feb 19 '11 at 10:09
  • I think she meant "Best possible method" – Ananth Feb 19 '11 at 13:13

15 Answers15

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

string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
FUD
  • 5,114
  • 7
  • 39
  • 61
Karel
  • 2,112
  • 1
  • 15
  • 25
29

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");
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
20

As everyone else said, but remember CultureInfo.InvariantCulture!

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

OR escape the '/'.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • What is the purpose of InvariantCulture here? –  Feb 19 '11 at 10:17
  • 3
    Not all the cultures use the / as a date separator. Some write dd-MM-yyyy. The framework converts the / in your format string to the date separator of the current culture (current at the time of execution). But you want to always use the / separator. The second option (OR escape...) is to "escape" the /, so that ToString doesn't consider it as a date separator but as a "fixed" char. You escape with '\', so it would be @"dd\/M\/yyyy" (the @ is because otherwhise you would have to escape the \, so "dd\\/M\\/yyyy"). All of them are quite unreadable, so it's better to fix the CultureInfo. – xanatos Feb 19 '11 at 10:21
11

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

DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
josliber
  • 43,891
  • 12
  • 98
  • 133
Hemanth
  • 177
  • 2
  • 11
10

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('-','/');
Siddhesh Adarkar
  • 119
  • 2
  • 11
7

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

DateTime.ToString("dd/MM/yyyy");
CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
smruti swarup
  • 71
  • 1
  • 1
4

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(); 
}
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
awaispk
  • 41
  • 2
3

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");
Smur
  • 3,075
  • 7
  • 28
  • 46
3

In C# 10 you can use DateOnly.

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

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
1

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

If you want the string use -

DateTime.ToString("dd/MM/yyyy")
Jason Quinn
  • 2,443
  • 3
  • 28
  • 36
1
string currentdatetime = DateTime.Now.ToString("dd'/'MM'/'yyyy");
THess
  • 1,003
  • 1
  • 13
  • 21
0
DateTime.Parse(YOUR_DATE_OBJECT).ToShortDateString();

ToShortDateString() method will help you convert DateTime To Just Date String,format dd/mm/yyyy.

Xahiid
  • 61
  • 6
-1

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);
joan16v
  • 5,055
  • 4
  • 49
  • 49
Mahesh
  • 157
  • 2
  • 8
-6

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;
GLOBAL TECH
  • 71
  • 10