2

I have ListView with DateTime field which displaying 00:00:00 on Label, that is not required at all.

public DateTime? StartDate
{
    get { return _startDate; }
    set
    {
        SetProperty(ref _startDate, value);
        //StartDate = DateTime.ParseExact(StartDate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture); NOT WORKING
    }
}

I tried to remove it using below line but not working

StartDate = DateTime.ParseExact(StartDate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture); //NOT WORKING

The code displaying it

<StackLayout Orientation="Horizontal">
    <Label HorizontalOptions="Center" VerticalOptions="Center" Text="{Binding StartDate}" />
    <Label HorizontalOptions="Center" VerticalOptions="Center" Text="To" />
    <Label HorizontalOptions="Center" VerticalOptions="Center" Text="{Binding EndDate}" />
</StackLayout>

ListCell:

enter image description here

How can I remove 0's from date:

R15
  • 13,982
  • 14
  • 97
  • 173
  • 4
    You want to format it in display, not in value. You can use metadata like `[DataType(DataType.Date)]` above the property declaration – Rafalon Jun 07 '19 at 07:10
  • @Rafalon - Pl check my updated question. – R15 Jun 07 '19 at 07:13
  • Thank you, also check my updated comment ^^ – Rafalon Jun 07 '19 at 07:14
  • Look at [String formatting](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/string-formatting) for Xamarin.Forms – MindSwipe Jun 07 '19 at 07:14
  • date.ToShortDateString(); or date.ToString("dd-MM-yyyy"); any of these method will work – Claudio Corchez Jun 07 '19 at 07:14
  • related : https://stackoverflow.com/questions/32814176/how-to-format-date-and-time-in-xaml-in-xamarin-application, https://stackoverflow.com/questions/685743/how-to-use-stringformat-in-xaml-elements, https://stackoverflow.com/questions/447035/what-is-the-wpf-xaml-data-binding-equivalent-of-string-format – xdtTransform Jun 07 '19 at 07:45

4 Answers4

4

This is not how DateTime works in C#, let me give an example:

// This will print '01.01.2000 00:00:00'
Console.WriteLine(DateTime.ParseExact("01/01/1970", "dd/MM/yyy", CultureInfor.InvariantCulture));

This is expected, because every property in the DateTime struct has a value after the constructor, 00:00:00 is just the default value. What you need to do is format it where you're displaying it (so in your label), using StringFormat and a format specifier for DateTime, the one you probably want is d. So your xaml should like this:

<StackLayout Orientation="Horizontal">
    <Label HorizontalOptions="Center" VerticalOptions="Center" Text="{Binding StartDate, StringFormat='{0:d}'}" />
    <Label HorizontalOptions="Center" VerticalOptions="Center" Text="To" />
    <Label HorizontalOptions="Center" VerticalOptions="Center" Text="{Binding EndDate, StringFormat='{0:d}'}" />
</StackLayout>
MindSwipe
  • 7,193
  • 24
  • 47
2

You have to format the Binding on XAML

https://blogs.msdn.microsoft.com/vsdata/2009/07/06/customize-format-of-datetime-string-in-wpf-and-winform-data-binding/

https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

<Label HorizontalOptions="Center" VerticalOptions="Center" Text="{Binding StartDate, StringFormat='{0:d}'}" />

for example.

blfuentes
  • 2,731
  • 5
  • 44
  • 72
0

Use StartDate.ToShortDateString() instead of StartDate.ToString(). ToShortDateString returns a string that contains the short date string representation of the current DateTime object.

Pooja Kamath
  • 1,290
  • 1
  • 10
  • 17
0

date.ToString("d") There are other formats but "d" is the short date. You need to change the output string, not the DateTime itself. The type DateTime contains the Date and the Time, as the name implies and removing the Time is not possible but you don't need to use the time.

JP-Hundhausen
  • 70
  • 1
  • 7