views:

93

answers:

5

I have the date format string dd-mm-yy. Please can you tell me how to add hours and minutes to the string (i.e 13-03-2010.21.03) ....

DateTime.Today.ToString("dd-mm-yy") ?
+1  A: 

You can use this:

DateTime.Now.ToString("dd-MM-yy.hh.mm")

If you want 24 hours:

DateTime.Now.ToString("dd-MM-yy.HH.mm")

See the MSDN documentation.

Note that the format string you first used is incorrect, as mm stands for minutes so you should have used MM for months.

You are also using Today, which does not have the time component (so it would always be 00:00:00). If you need those (hours, minutes, seconds and milliseconds), you should use Now.

Oded
Why the downvote?
Oded
mm is minutes, not month.
Matthew Flaschen
@Matthew Flaschen - And he was asking for minutes to be added. Answer amended.
Oded
i wasn't the one behind the d/v so i can't answer, i guess, but for one thing, `Today` will always yield `00.00`
David Hedlund
@David Hedlund - Good point. Answer amended.
Oded
+2  A: 

please check this http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx and http://www.csharp-examples.net/string-format-datetime/ for more information

String.Format("{0:d/M/yyyy HH:mm:ss}", datetime)
Space Cracker
+1 for providing the correct links
GvS
He wants 2-digit day, month, and he doesn't want seconds.
Matthew Flaschen
+2  A: 
DateTime.Now.ToString("dd-MM-yyyy.HH.mm");

mm is minutes, MM is months, so your current example is wrong.

Also, note that you need to use Now rather than Today, otherwise it'll always be 00.00

David Hedlund
It looks like he also wants 4-digit year (yyyy), despite his current code.
Matthew Flaschen
@Matthew Flaschen: good catch! edited.
David Hedlund
+1  A: 
DateTime.Now.ToString("dd-MM-yy.HH.mm");

MM for Months mm for minutes

kawtousse
+2  A: 
DateTime.Now.ToString("dd-MM-yy.HH.mm")

Change the Today to Now

Please note that mm = minutes and MM = months

GvS