I got the current datetime by using the following code.
DateTime dt = DateTime.Now;
But it is in the format of, 2015-02-23 17:25:07.123 how to convert this to the format of, "02/23/2015"?
"But it is in the format of"
No it isn't. It's just a DateTime
. If you want a particular text representation, call ToString
on it, specifying the format. For example:
DateTime now = DateTime.Now;
string formatted = now.ToString("MM/dd/yyyy");
You might also want to specify the culture to use for formatting - that way you could just say "Use the right short date format" for example.
See the MSDN pages on custom date/time format strings and standard date/time format strings for more details.
See more on this question at Stackoverflow