Format Date/Time in XAML in Silverlight
Asked Answered
B

6

79

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?

Bilbrey answered 7/4, 2011 at 17:14 Comment(0)
S
131
<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)

Subchaser answered 7/4, 2011 at 17:29 Comment(5)
I love these F~€#~€ weird string formats ... who the hell can recall them (talking about the {}0: part).Rotund
This is correct, except your quotes around the format string should be single quotes <TextBlock Text="{Binding Date, StringFormat={}{0:'MM/dd/yyyy a\\t h:mm tt'}}" />Wightman
I had to put my single quotes in a different place: Text="{Binding startDate, StringFormat='{}{0:MM/dd/yy}'}"Narcissus
If you're using a Label rather than TextBlock, recall that you need to use ContentStringFormat instead of setting the Binding StringFormat.Adoration
all these work for a textblock or textbox but inside a datepicker itself the formats does not work. Any idea how to do it in the date pickerBurk
H
66

You can use StringFormat in Silverlight 4 to provide a custom formatting of the value you bind to.

Dates

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”
Hefter answered 7/4, 2011 at 17:18 Comment(0)
L
11

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

enter image description here

Laxation answered 24/6, 2013 at 3:19 Comment(2)
What if I want Day/Month/Year?Victim
Use StringFormat = dd-MMMM-yyLaxation
C
7

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\}'}">
Cowfish answered 3/5, 2013 at 12:37 Comment(0)
E
6

C#: try this

  • yyyy(yy/yyy) - years
  • MM - months(like '03'), MMMM - months(like 'March')
  • dd - days(like 09), ddd/dddd - days(Sun/Sunday)
  • hh - hour 12(AM/PM), HH - hour 24
  • mm - minute
  • ss - second

Use some delimeter,like this:

  1. MessageBox.Show(DateValue.ToString("yyyy-MM-dd")); example result: "2014-09-30"
  2. empty format string: MessageBox.Show(DateValue.ToString()); example result: "30.09.2014 0:00:00"
Elle answered 1/10, 2014 at 7:55 Comment(1)
MessageBox.Show(DateValue.ToString()) this output depends on the culture of Thread executing it (if not set its the system culture)Andros
H
2

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"/>
Hereinto answered 11/2, 2021 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.