Различие между двумя DateTimes C#?

Если вы используете GTK, то у вас также есть Gtk.gtk_show_uri_on_window(), который использует GLib под капотом.

88
задан S.L. Barth - Reinstate Monica 23 July 2012 в 08:10
поделиться

5 ответов

Вы можете сделать следующее:

TimeSpan duration = b - a;

В классе timespan есть множество встроенных методов, чтобы делать то, что вам нужно, т.е.

duration.TotalSeconds
duration.TotalMinutes

Более подробную информацию можно найти здесь .

172
ответ дан 24 November 2019 в 07:30
поделиться

Try the following

double hours = (b-a).TotalHours;

If you just want the hour difference excluding the difference in days you can use the following

int hours = (b-a).Hours;

The difference between these two properties is mainly seen when the time difference is more than 1 day. The Hours property will only report the actual hour difference between the two dates. So if two dates differed by 100 years but occurred at the same time in the day, hours would return 0. But TotalHours will return the difference between in the total amount of hours that occurred between the two dates (876,000 hours in this case).

The other difference is that TotalHours will return fractional hours. This may or may not be what you want. If not, Math.Round can adjust it to your liking.

17
ответ дан 24 November 2019 в 07:30
поделиться
int hours = (int)Math.Round((b - a).TotalHours)
1
ответ дан 24 November 2019 в 07:30
поделиться

Возможно, вы ищете:

int Difference = (a-b).Hours;
3
ответ дан 24 November 2019 в 07:30
поделиться
var theDiff24 = (b-a).Hours
0
ответ дан 24 November 2019 в 07:30
поделиться
Другие вопросы по тегам:

Похожие вопросы: