Take the 2-minute tour ×

i have an object 2/19/2011 12:00:00 AM. i want to convert this object to 19/2/2011. please give me the most possible answers

convert datetime to string format.

share|improve this question
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
Do you want string formatting, or do you want just the date? –  Jean Hominal Feb 19 '11 at 10:04
Please make your question much, much clearer. Read tinyurl.com/so-hints –  Jon Skeet Feb 19 '11 at 10:05
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

8 Answers 8

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

string s = dt.ToString("dd/M/yyyy");
share|improve this answer
what about if user enter any format. like 14 jan 1988 –  SANDEEP Sep 4 '13 at 6:12
Sandeep, then you'll have to use the DateTime.TryParse() method. –  MCattle Sep 9 '13 at 15:04

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");
share|improve this answer

As everyone else said, but remember CultureInfo.InvariantCulture!

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

OR escape the '/'.

share|improve this answer
What is the purpose of InvariantCulture here? –  user166390 Feb 19 '11 at 10:17
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

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(); 
share|improve this answer

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

share|improve this answer

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('-','/');
share|improve this answer

If you want the string use -

share|improve this answer

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


DateTime date = new DateTime(2011, 02, 19);
string strDate = date.ToString("dd/MM/yyyy");
share|improve this answer

Your Answer


