views:

1137

answers:

5

I have a DateTime class and wish to display it according to some format... in this case, I specifically want to format it as YYYYMMDD format.

What's the best C#/.NET API function for doing this?

+1  A: 

DateTime.ToString()

Look at the docs for the format string details.

Will Dean
+4  A: 

ToString(format)? i.e.

string s = DateTime.Today.ToString("yyyyMMdd");

(note the case)

Marc Gravell
+5  A: 

I always use this site to get any dates formats etc. http://blog.stevex.net/index.php/string-formatting-in-csharp/

Peanut
+1  A: 
DateTime.Now.ToString("yyyyMMdd");
Darin Dimitrov
+2  A: 
myDateTime.ToString("yyyyMMdd")

Here you can find a comprehesive list of DateTime patterns and pattern characters: DateTime.ToString() Patterns

splattne