2

Im struggling to check if there is at least a minute between two date times. I created a game in c# and have limited a part of my game to once a minute every time this command is executed it runs a void

The problem is that it does it even if it hasn't been a minute?

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;
    }
}

EDIT: I have decided to use timespan. But when I try to get the seconds of the timespan after it has reached 60 it resets?

Ash Smith
  • 335
  • 1
  • 4
  • 6
  • possible duplicate of [C# Compare Time between Two Time Intervals](http://stackoverflow.com/questions/10631044/c-sharp-compare-time-between-two-time-intervals) – Steve Tauber Aug 08 '14 at 23:16
  • 1
    what are your values for `TimeStamp_Converted` and `DateTime.UtcNow` – lincolnk Aug 08 '14 at 23:31

2 Answers2

5

Try

if ((DateTime.UtcNow - TimeStamp_Converted).TotalMinutes > 1)
cost
  • 4,420
  • 8
  • 48
  • 80
  • I have changed it to timespan, just removed the timespan from the datetime now? But i have a problem. When doing the .Seconds on the timespan it resets to 0 after a minute? And .TotalMinutes returns a long decimal – Ash Smith Aug 08 '14 at 23:40
  • Im sorry but I do not understand what you just commented? – Ash Smith Aug 08 '14 at 23:41
  • @AshSmith .Seconds will only return the seconds portion of the time. So if the TimeSpan is a minute and thirty seconds, it will only return 30. TotalMinutes will return that TimeSpan as minutes. A minute and thirty seconds will be 1.5 TotalMinutes – cost Aug 09 '14 at 00:38
  • @AshSmith If you just want the total number of seconds, use `TotalSeconds` – cost Aug 09 '14 at 00:39
1

It should be:

if (TimeStamp_Converted < DateTime.UtcNow.AddMinutes(-1))
Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37