Ich möchte zwei Daten mit der Zeit subtrahieren. Am Ende werde ich etwa 44 Tage, 14 Stunden, 15 Minuten bekommen. Jetzt bin ich nur Zeit bekommen 02.03.00 (die Sekunden ich nicht brauchen.)Datetime subtrahieren uwp
Das Datum und die Zeit, die ich bin takeing von picker und Zeitauswahl
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();
}