0

I have a curious issue with the DatePicker View regarding DateTime Formats. My goal is to get the Date and Time starting at 00:00 (24 hours) format but I always get the Time as 12:00 AM (12 hours) format. That code is in the Xamarin.Forms PCL library:

        <DatePicker x:Name="TruckAuftragBeginDatetime" 
                  Format="dd.MM.yyyy"
                  TextColor="Aqua"
                  BackgroundColor="Black"
                  VerticalOptions="CenterAndExpand"
                  HorizontalOptions="EndAndExpand"
                  DateSelected="TruckAuftragBeginDatetimeOnDateSelected"></DatePicker>

I even tried to set Format as Format="dd.MM.yyyy HH:mm" with the same result in my DateSelected EventHandler. I always get something like that:

e.NewDate {11/30/2016 12:00:00 AM} Date: {System.DateTime} Day: 30 DayOfWeek: System.DayOfWeek.Wednesday DayOfYear: 335 Hour: 0 Kind: System.DateTimeKind.Unspecified Millisecond: 0 Minute: 0 Month: 11 Second: 0 Ticks: 636160608000000000 TimeOfDay: {System.TimeSpan} Year: 2016

sinceDateTime {11/30/2016 1:00:00 AM} Date: {System.DateTime} Day: 30 DayOfWeek: System.DayOfWeek.Wednesday DayOfYear: 335 Hour: 1 Kind: System.DateTimeKind.Local Millisecond: 0 Minute: 0 Month: 11 Second: 0 Ticks: 636160644000000000 TimeOfDay: {System.TimeSpan} Year: 2016

My Code (EventHandler):

    private async void TruckAuftragBeginDatetimeOnDateSelected(object sender, DateChangedEventArgs e)
    {
        var sinceDateTime = e.NewDate.ToLocalTime();
    }

As a temporary workaround I take the LocalTime which is one hour ahead in my timezone.

Question:

How can I get the datetime in 24 hour format with a result like:

e.NewDate {11/30/2016 00:00:00} Date: {System.DateTime} Day: 30 DayOfWeek: System.DayOfWeek.Wednesday DayOfYear: 335 Hour: 0 Kind: System.DateTimeKind.Unspecified Millisecond: 0 Minute: 0 Month: 11 Second: 0 Ticks: 636160608000000000 TimeOfDay: {System.TimeSpan} Year: 2016

Erich Brunner
  • 612
  • 5
  • 19
  • Hello, I have not tested it to be honest , but maybe it depends on the device's settings , have you tried changing that time format there ? – Ahmad ElMadi Dec 01 '16 at 07:38
  • I'm quite sure it depends on the device Locale and settings – Stephane Delcroix Dec 01 '16 at 08:03
  • http://stackoverflow.com/questions/30187742/xamarin-forms-timepicker-24hour/35627311?noredirect=1#comment62380618_35627311 Check this – Mr.Koçak Dec 01 '16 at 08:06
  • Thank you very much for all Your suggestions. I guess Mr. Kovac's added link is the only way. I tried setting to german language in the phone and that didn't change anything. What I really don't understand is that even when I instantiate a brand new DateTime with – Erich Brunner Dec 01 '16 at 14:11
  • var date = new DateTime(2016,12,1,0,0,0) I get 1.12.2016 12:00 AM and not 1.12.2016 00:00 . I a standard console app I get 1.12.2016 00:00 (24 hour format) hmm... – Erich Brunner Dec 01 '16 at 14:13
  • What are you trying to do? Do you want to display your DateTime in certain format or you want to Parse it to certain string format? – Yuri S Dec 02 '16 at 19:58

2 Answers2

0

Try this return NewDate.Value.ToString("HH:mm");

Adit Kothari
  • 421
  • 3
  • 16
  • Thank you very much Adit. I need the DateTime instance. I tried that with ToString but even when I try to parse it back in a DateTime instance with DateTime.ParseExact(date,"ddMMyyyyHHmm") I get a 12 hour formatted date like 02.12.2016 12:00 AM. I guess I have to to it like Mr. Kovacs commented. – Erich Brunner Dec 02 '16 at 13:07
0

Screenshot with code and watch window:

enter image description here

Yuri S
  • 5,355
  • 1
  • 15
  • 23
  • Hi Yuri, I take a date from the DatePicker and use it in a Linq query as a lower bound daterange to filter data by that date. For instance I pick 01.12.2016, then I want to show only data records since 01.12.2016 00:00:00 . With the DatePicker date I get 01.02.2016 12:00 AM which would be a date at high noon (middle of day) to my data records date. Data record date is a Azure Mobile Apps UpdatedAt field of type DateTimeOffset? of a Offline Sync table record. – Erich Brunner Dec 02 '16 at 20:28
  • I still don't get it. If you need DateTime object just use one you receive from picker. If you need string I showed you how to convert it to 24-hours format string. if you need to create DateTimeOffset use constructor and pass DateTime from picker. What exactly doesn't work? – Yuri S Dec 02 '16 at 20:32
  • Date records DatetimeOffset? is in 24 hour format . And the DatePicker date is in 12 hour format. I want date of DatePicker in 24 hour format. – Erich Brunner Dec 02 '16 at 20:35
  • DatePicker date is a DATE. It is not in any format till you parse it to string. You can use it directly – Yuri S Dec 02 '16 at 20:36
  • Ahhmm.. So a DateTimeOffset with value "01.12.2016 00:00:00" == "01.12.2016 12:00 AM" ? – Erich Brunner Dec 02 '16 at 20:44
  • You are comparing strings not DateTime objects. Strings can be formatted as you wish. If you format BOTH objects to the same format you can compare strings. – Yuri S Dec 02 '16 at 20:47
  • No that was a pseudo code. I mean I have a DateTimeOffset? date1 which is in 24 hour format (value: 01.12.2016 00:00:00) and the DatePicker date with value 01.12.2016 12:00 AM. you mean date1.Equals(datepickerDate) == true. I don't compare string values because my Linq query is something like: OfflinesyncTable.Where(ta => ta.UpdatedAt >= datePickerDate) – Erich Brunner Dec 02 '16 at 20:53
  • It does'n make sense to say "date1 which is in 24 hour format". Date doesn't have format. Try to compare 2 dates and you will see the result – Yuri S Dec 02 '16 at 21:06
  • Thanks for you inputs ! – Erich Brunner Dec 02 '16 at 21:09
  • The answer like Yuri wrote : [Compare Dates in Different Formats](http://stackoverflow.com/questions/26716608/compare-two-different-formats-of-the-datetime) – Erich Brunner Dec 02 '16 at 21:21
  • As the link you posted says:"...DateTime format is irrelevant." That is what I was trying to tell. You can accept an answer then. – Yuri S Dec 02 '16 at 21:25