Kjell Derous
Kjell Derous

Reputation: 522

Formatting date (time) in a string

I have a very stupid question.

When the end-user plans an action, the planned time must be visible on the usercontrol of the action like this (hh:mm). On every site I find about formatting dates into a string, they say to use {0:h} or {0:hh}. In my following code I do the same thing, it just doesn't work.

When the end-user plans an action, the returned string is now "Planned Start Date - HH:DD".

lblStartDatePlanned.Content = String.Format("Planned Start Date - {0:hh}:{1:dd}", date.Hour, date.Minute);

Object date is of type DateTime.

Anyone knows what is wrong? I don't want to waste too much time on such a small thing. Thanks!

Upvotes: 1

Views: 146

Answers (3)

Palak.Maheria
Palak.Maheria

Reputation: 1515

Try following:

lblStartDatePlanned.Content = date.ToString(@"hh\:mm");

Upvotes: 1

David Hoerster
David Hoerster

Reputation: 28701

Generically, if you're trying to force 2 digits in a format string, you can also use:

String.Format("Planned Start Date - {0:00}:{1:00}", date.Hour, date.Minute);

Although, @Alessandro's answer is correct for your problem.

Upvotes: 1

Alessandro D'Andria
Alessandro D'Andria

Reputation: 8868

Use:

lblStartDatePlanned.Content = String.Format("Planned Start Date - {0:HH:mm}", date);

Upvotes: 6

Related Questions