56

I have a set up XAML code below.

<Label Text="{Binding Date}"></Label>
<Label Text="{Binding Time}'}"></Label>

I want result like september 12,2014 2:30 PM.

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
Narendra
  • 1,332
  • 2
  • 13
  • 16
  • Looks as though you've fogotten to add your xaml code! – Fuzzy Analysis Sep 28 '15 at 00:20
  • 1
    The answers below are probably closer to what you are looking for, but you should note that Xamarin Forms has a bug in regards to the formatting of dates in binding. https://forums.xamarin.com/discussion/comment/290295/#Comment_290295 – Christian Findlay Aug 08 '17 at 00:42
  • @MelbourneDeveloper I saw your thread and issue was moved to GitHub. Thank you very much for you contribution. Unfortunately, as of today, it is still not solved. I will just leave a link to the GitHub- Issue here so people can be up to date. https://github.com/xamarin/Xamarin.Forms/issues/2049 – Vandrey Mar 29 '20 at 16:34

4 Answers4

179

Change your code to:

<Label Text="{Binding Date, StringFormat='{0:MMMM dd, yyyy}'}"></Label>
<Label Text="{Binding Time, StringFormat='{}{0:hh\\:mm}'}"></Label>
JKennedy
  • 18,150
  • 17
  • 114
  • 198
13

Make a custom IValueConverter implementation:

public class DatetimeToStringConverter : IValueConverter
{
    #region IValueConverter implementation

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

        var datetime = (DateTime)value;
        //put your custom formatting here
        return datetime.ToLocalTime().ToString("g");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException(); 
    }

    #endregion
}

Then use it like that:

<ResourceDictionary>
    <local:DatetimeToStringConverter x:Key="cnvDateTimeConverter"></local:DatetimeToStringConverter>
</ResourceDictionary>

...

<Label Text="{Binding Date, Converter={StaticResource cnvDateTimeConverter}}"></Label>
<Label Text="{Binding Time, Converter={StaticResource cnvDateTimeConverter}}"></Label>
Daniel Luberda
  • 7,374
  • 1
  • 32
  • 40
  • I try your code, but getting invalid cast exception on the line, var datetime = (DateTime)value; For me receiving the date as a java timestamp: 1510822596449. Is your code working for fine for converting java timestamp to string date? –  Dec 07 '17 at 05:33
  • Me too, i tried your code then i got a invalid cast exception. – rudi Ladeon Sep 08 '20 at 16:31
9
<Label>

<Label.FormattedText>

<FormattedString>

<Span Text="{Binding Date, StringFormat='{0:MMMM dd, yyyy}'}"/>
<Span Text=" "/>
<Span Text="{Binding Time, StringFormat='{0:h:mm tt}'}"/>

</FormattedString>

</Label.FormattedText>

</Label>
Mini Titan
  • 311
  • 4
  • 7
6

Use the standard .NET Date Format specifiers.

To get

September 12, 2014 2:30 PM

use something like

MMMM d, yyyy h:mm tt
Jason
  • 86,222
  • 15
  • 131
  • 146