I have a TextBlock
that is bound to a DateTime
property. How do I configure the format of the date?
73
votes
3 Answers
144
votes
36
votes
If you want to use a common format string between bindings, you could declare the binding like this:
<Textbox Text={Binding Path=DateTimeValue, StringFormat={x:Static local:Constants.DateTimeUiFormat}} />
With your constants class like this:
public static class Constants
{
public const string DateTimeUiFormat = "dd/MM/yyyy";
//etc...
}
24
votes
May be helpful to someone:
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now},
StringFormat='{}{0: Today is dddd, MMMM dd, yyyy, hh:mm:ss}'}"/>
or 24h and 2digits month and year format:
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now},
StringFormat='{}{0: Today is dddd, MM.dd.yy, HH:mm:ss}'}"/>