Reputation: 11151
I have a Silverlight 4 application. I have a C# DateTime object that I'm binding to my UI. I want this DateTime to be in the format of dd/mm/yyyy
time (TimeZone). For instance, today would be displayed as
04/07/2011 at 01:13 p.m. (EST)
Is there a way to do this XAML
? Or do I need to build a converter?
Upvotes: 79
Views: 125840
Reputation: 750
For me this worked:
<TextBlock Text="{Binding Date , StringFormat=g}" Width="130"/>
If want to show seconds also using G instead of g :
<TextBlock Text="{Binding Date , StringFormat=G}" Width="130"/>
Also if want for changing date type to another like Persian , using Language :
<TextBlock Text="{Binding Date , StringFormat=G}" Width="130" Language="fa-IR"/>
Upvotes: 2
Reputation: 160992
You can use StringFormat in Silverlight 4 to provide a custom formatting of the value you bind to.
The date formatting has a huge range of options.
For the DateTime of “April 17, 2004, 1:52:45 PM”
You can either use a set of standard formats (standard formats)…
StringFormat=f : “Saturday, April 17, 2004 1:52 PM”
StringFormat=g : “4/17/2004 1:52 PM”
StringFormat=m : “April 17”
StringFormat=y : “April, 2004”
StringFormat=t : “1:52 PM”
StringFormat=u : “2004-04-17 13:52:45Z”
StringFormat=o : “2004-04-17T13:52:45.0000000”
… or you can create your own date formatting using letters (custom formats)
StringFormat=’MM/dd/yy’ : “04/17/04”
StringFormat=’MMMM dd, yyyy g’ : “April 17, 2004 A.D.”
StringFormat=’hh:mm:ss.fff tt’ : “01:52:45.000 PM”
Upvotes: 66
Reputation: 61
C#: try this
Use some delimeter,like this:
Upvotes: 6
Reputation: 89735
<TextBlock Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy a\\t h:mm tt}'}" />
will return you
04/07/2011 at 1:28 PM (-04)
Upvotes: 131
Reputation: 5708
you can also use just
StringFormat=d
in your datagrid column for date time showing
finally it will be
<sdk:DataGridTextColumn Binding="{Binding Path=DeliveryDate,StringFormat=d}" Header="Delivery date" Width="*" />
the out put will look like
Upvotes: 11
Reputation: 6636
In SL5 I found this to work:
<TextBlock Name="textBlock" Text="{Binding JustificationDate, StringFormat=dd-MMMM-yy hh:mm}">
<TextBlock Name="textBlock" Text="{Binding JustificationDate, StringFormat='Justification Date: \{0:dd-MMMM-yy hh:mm\}'}">
Upvotes: 7