Search code examples
datetimewin-universal-appsubtraction

Datetime subtract uwp


I want to subtract two dates with time. That in the end, I will get something like 44 days, 14 hours, 15 minutes. Now I am getting only time 02:03:00 (the seconds I don´t need.)

The date and time I am takeing from datepicker and time picker

Xaml code

        <StackPanel HorizontalAlignment="Left">
            <!--<TextBlock x:Name="textblock" Text="textblock"/>-->

            <DatePicker x:Name="datepicker1" Margin="10"/>
            <TimePicker x:Name="timepicker1" Margin="10"/>
            <DatePicker x:Name="datepicker2" Margin="10"/>
            <TimePicker x:Name="timepicker2" Margin="10"/>
            <Button x:Name="datumbutton" Click="datumbutton_Click" Content="Combine"/>
            <TextBlock x:Name="datumtextblock" Margin="20" Text="datum" />
            <TextBlock x:Name="datumtextblock2" Text="datum2"/>
            <TextBlock x:Name="vysledek" Text="vysledek"/>
        </StackPanel>

UWP code

using Windows.Globalization;
using Windows.Globalization.DateTimeFormatting;        

private void datumbutton_Click(object sender, RoutedEventArgs e)
            {
                DateTimeFormatter dateFormatter = new DateTimeFormatter("shortdate");
                DateTimeFormatter timeFormatter = new DateTimeFormatter("shorttime");

                Calendar calendar = new Calendar();
                calendar.ChangeClock("24HourClock");

                DateTimeOffset selectedDate = this.datepicker1.Date;
                DateTimeOffset combinedValue = new DateTimeOffset(new DateTime(selectedDate.Year, selectedDate.Month, selectedDate.Day) + this.timepicker1.Time);

                DateTimeOffset selectedDate2 = this.datepicker2.Date;
                DateTimeOffset combinedValue2 = new DateTimeOffset(new DateTime(selectedDate.Year, selectedDate.Month, selectedDate.Day) + this.timepicker2.Time);

                calendar.SetDateTime(combinedValue);
                calendar.SetDateTime(combinedValue2);


                datumtextblock.Text= ("Combined value: " + dateFormatter.Format(combinedValue) + " " + timeFormatter.Format(combinedValue));
                datumtextblock2.Text = ("Combined value: " + dateFormatter.Format(combinedValue2) + " " + timeFormatter.Format(combinedValue2));
                System.TimeSpan odecet = combinedValue.Subtract(combinedValue2);
                vysledek.Text = odecet.ToString();

            }

Solution

  • There is a minor negligence in your code. When getting combinedValue2, you should use

    DateTimeOffset combinedValue2 = new DateTimeOffset(new DateTime(selectedDate2.Year, selectedDate2.Month, selectedDate2.Day) + this.timepicker2.Time);
    

    instead of

    DateTimeOffset combinedValue2 = new DateTimeOffset(new DateTime(selectedDate.Year, selectedDate.Month, selectedDate.Day) + this.timepicker2.Time);
    

    After this, you can use something like following to get what you want.

    vysledek.Text = $"{odecet.Days} days, {odecet.Hours} hours, {odecet.Minutes} minutes";
    

    If you want to always get the absolute value, you can use TimeSpan.Duration Method like var absoluteOdecet = odecet.Duration();.