0
我想用时间减去两个日期。最后,我会得到像44天,14小时,15分钟的东西。现在我只得到时间2时03分00秒(秒我不需求。)日期时间减去uwp
的日期和时间,我从日期选择器和时间选择器将士
XAML代码
<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代码
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();
}