C#检查两个日期时间之间是否有一分钟

时间:2014-08-08 23:12:14

标签: c# datetime datetime-comparison

我很难检查两个日期之间是否至少有一分钟的时间。我在c#中创建了一个游戏,并且每次执行此命令时,我的游戏的一部分都限制在每分钟一次 执行它运行一个空白

问题在于它即使不是一分钟也会这样做吗?

public void _CheckIfBeenAMinute
{
    string TimeStamp;

    using (IQueryAdapter dbClient = SilverwaveEnvironment.GetDatabaseManager().getQueryreactor())
    {
        dbClient.setQuery("SELECT game_timestamp FROM users WHERE id=" + Session.Id + "");
        TimeStamp = dbClient.getString();
    }

    DateTime TimeStamp_Converted = Convert.ToDateTime(TimeStamp);

    if (TimeStamp_Converted > DateTime.UtcNow.AddMinutes(-1))
    {
        //It has been a minuted...
        //But the problem is, i it hasnt been it still does this?

        this.SendMessage("You have reached your limit today");
        return;
    }
}
编辑:我决定使用时间跨度。但是当我试图在达到60之后获得时间跨度时它会重置吗?

2 个答案:

答案 0 :(得分:4)

尝试

if ((DateTime.UtcNow - TimeStamp_Converted).TotalMinutes > 1)

答案 1 :(得分:1)

应该是:

if (TimeStamp_Converted < DateTime.UtcNow.AddMinutes(-1))