tags:

views:

103

answers:

1

Is there a easy way to convert a DateTime object to a string representation like this '2010-03-03 10:38:48'.

I'm not sure what the above format is but it is different from the patterns obtainable via ToLongTimeString() etc.

Is this a case for string builder?

+11  A: 

Like this:

date.ToString("yyyy-MM-dd hh:mm:ss")

If you want 24 hour time, replace hh with HH.

For more information, see here.

SLaks
I think you want lower case "mm" for the minutes.
Jarod Elliott
@Jarod: Fixed; thanks:
SLaks
Thanks, worked fine with uppercase btw will change for aesthetic reasons though.
Chin
@Chin: It did not work fine with uppercase. An uppercase `MM` will give months, not minutes.
SLaks
@SLaks - Yes you are right. Looked good the first time I looked at it.
Chin
Bookmark that link, it's incredibly useful.
Alastair Pitts