views:

308

answers:

4

DateTime's formatting has some overlap between it'a standard date and time formatting strings and some of it's custom format specifier by them selves. As a result, when I evalate this expression:

string.Format(">{0:d}< >{0: d}<", DateTime.Now)

and get this result:

>8/3/2009< > 3<

My question is: How do I get String.Format to output just the day of the month (using the d format) without any surrounding spaces?

+4  A: 

use "{0:%d}": reference

BCS
A: 

DateTime.Now.Day.ToString()

Bob King
Simple solution, but likely this is part of a larger `String.Format()`, in which case I would personally prefer using `{0:%d}`.
Thorarin
+2  A: 

I keep this page in my favorites...

http://blog.stevex.net/index.php/string-formatting-in-csharp/

Saves me all the time.

Jeffrey Hines
A: 

DateTime.Now.ToString("dd");

Cyril Gupta
That give the day of the week: e.i. "wed"
BCS
Weirdly I thought that's what you wanted. I fixed the code.
Cyril Gupta
that gives: "8/3/2009"
BCS
I changed it again. It works... This was really comic :) Me making mistakes and you pointing them out.
Cyril Gupta