检查经过了多少小时/天

时间:2018-09-08 23:11:16

标签: c# datetime time


我正在尝试创建一个每小时和每天都授予奖励的功能。

为此,我想实现一个给定2 DateTime的功能,检查经过了多少小时/天。

我已经有一个协程可以每秒调用一次该函数。

例如,我想每00:00:00、01:00:00 ... 12:00:00 ... 23:00:00授予奖励


示例:

DateTime date0 = new DateTime(2018, 8, 9, 20, 59, 59);
DateTime date1 = new DateTime(2018, 8, 9, 21, 0, 0);
int hoursPassed = (int) date1.Subtract(date0).TotalHours;
Debug.Log("Hours "+ hoursPassed);

在这种情况下,通过的小时数将等于0。我想做一个函数(也许已经存在)来代替我1。日子也一样。


示例2

DateTime date0 = new DateTime(2018, 8, 9, 23, 59, 59);
    DateTime date1 = new DateTime(2018, 8, 10, 00, 00, 00);
    int DaysPassed = (int) date1.Subtract(date0).TotalDays;
    Debug.Log("Days "+ DaysPassed);

它将检索0,而我想检索1。


我也希望在经过多个小时/天之后才能正常工作。


有人可以帮助我吗?

谢谢

3 个答案:

答案 0 :(得分:0)

如果我对您的理解正确:

private static int GetHours(DateTime date1, DateTime date2)
{
    return Math.Max(0, Convert.ToInt32((date1 - date2).TotalMinutes / 60 + ((date1 - date2).TotalMinutes % 60 > 0 ? 1 : 0)));
}

用法:

    DateTime date0 = new DateTime(2018, 8, 9, 20, 59, 59);
    DateTime date1 = new DateTime(2018, 8, 9, 21, 0, 0);                        
    Console.Write(GetHours(date1, date0).ToString());

返回1

跨度超过一天:

    DateTime date0 = new DateTime(2018, 8, 9, 20, 59, 59);
    DateTime date1 = new DateTime(2018, 8, 10, 21, 0, 0);                       
    Console.Write(GetHours(date1, date0).ToString());

返回25

答案 1 :(得分:0)

最简单的方法是创建已修剪 DateTime变量,如果仅对小时感兴趣,则剥离分钟/秒;而对于仅对几天感兴趣,则修剪小时/分钟/秒。然后计算小时或天的差异。

// Calculates the number of hour strikes between the two given times
public static int HourStrikesBetween(DateTime from, DateTime to)
{
    if(from > to)
    {
        throw new ArgumentException("from must not be after to");
    }

    // Trim to hours
    DateTime fromTrimmed = new DateTime(from.Year, from.Month, from.Day, from.Hour, 0, 0);
    DateTime toTrimmed = new DateTime(to.Year, to.Month, to.Day, to.Hour, 0, 0);

    int hours = (int)(toTrimmed - fromTrimmed).TotalHours;

    return hours;
}

// Calculates the number of midnights between the two given times
public static int MidnightsBetween(DateTime from, DateTime to)
{
    if (from > to)
    {
        throw new ArgumentException("from must not be after to");
    }

    // Trim to days
    DateTime fromTrimmed = new DateTime(from.Year, from.Month, from.Day);
    DateTime toTrimmed = new DateTime(to.Year, to.Month, to.Day);

    int days = (toTrimmed - fromTrimmed).Days;

    return days;
}

示例:

DateTime date0 = new DateTime(2018, 8, 9, 20, 59, 59);
DateTime date1 = new DateTime(2018, 8, 9, 21, 0, 0);
int hourspassed = HourStrikesBetween(date0, date1);  // = 1

date0 = new DateTime(2018, 8, 9, 20, 00, 00);
date1 = new DateTime(2018, 8, 9, 21, 50, 0);
hourspassed = HourStrikesBetween(date0, date1);  // = still 1

date0 = new DateTime(2018, 8, 9, 20, 59, 59);
date1 = new DateTime(2018, 8, 9, 22, 10, 0);
hourspassed = HourStrikesBetween(date0, date1);  // = 2


date0 = new DateTime(2018, 8, 9, 23, 59, 59);
date1 = new DateTime(2018, 8, 10, 00, 00, 00);
int daysPassed = MidnightsBetween(date0, date1);  // = 1

请注意,如果您始终使用UTC DateTime而不是Local DateTime,则仅在跨越Dayligth Saving Time边界时才有效。

答案 2 :(得分:-1)

在我看来Math.Ceiling是完成这项工作的最简单方法:

示例:

DateTime date0 = new DateTime(2018, 8, 9, 20, 59, 59);
DateTime date1 = new DateTime(2018, 8, 9, 21, 0, 0);
int hoursPassed = (int)Math.Ceiling(date1.Subtract(date0).TotalHours);
Console.WriteLine("Hours " + hoursPassed);

示例2:

DateTime date0 = new DateTime(2018, 8, 9, 23, 59, 59);
DateTime date1 = new DateTime(2018, 8, 10, 00, 00, 00);
int DaysPassed = (int)Math.Ceiling(date1.Subtract(date0).TotalDays);
Console.WriteLine("Days "+ DaysPassed);