locked
compare date time, check for 24 hours or one day RRS feed

  • Question

  • User841411575 posted

    Hi,

    I need to compare two date times, Current date time and last date time of user login. Check if there has been 24hrs or a day since the user log in. 

    If there has been more than 24hrs or day since the user login. Then redirect to site, if not do nothing. 

    here is what I have so far:

    string domain = HttpContext.Current.User.Identity.Name;           
                using (Entities ent = new Entities())
                {
                    try
                    {
                        var query = ent.UsageTrackings.Where(emp => emp.Name == domain)
                            .OrderByDescending(emp => emp.Time).FirstOrDefault();
                            
                        if (query != null)
                        {
                            DateTime now = DateTime.Now;
                            DateTime Time = query.EntryTime;
                            if (query.Time.Date > now.AddHours(-24) && Time <= now)
                            {
                                Response.Redirect("http://google.com");
                            }                       
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }

    Right now is not checking correctly the last date time the user login. 

    Friday, January 20, 2017 3:01 PM

All replies

  • User-1716253493 posted
    if (query.Time < now.AddHours(-24))

    Friday, January 20, 2017 3:17 PM
  • User841411575 posted

    For some reason dont seems to work. Is not redirecting. Should I use AddDays instead of AddHours?

    Friday, January 20, 2017 3:54 PM
  • User-1716253493 posted

    i guess no defernt when add 1 day or 24 hours

    i change the operator "<"

    Try check the Time value

    Friday, January 20, 2017 5:13 PM
  • User-271186128 posted

    Hi newonehere69,

    You could set a break point to check the datetime values.

    Besides, you could use DateTime.Compare Method to compare the datetimes, code as below:

    public class Example
    {
       public static void Main()
       {
          DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
          DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
          int result = DateTime.Compare(date1, date2);
          string relationship;
    
          if (result < 0)
             relationship = "is earlier than";
          else if (result == 0)
             relationship = "is the same time as";         
          else
             relationship = "is later than";
    
          Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
       }
    }
    // The example displays the following output:
    //    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

    More details, see: https://msdn.microsoft.com/en-us/library/system.datetime.compare(v=vs.110).aspx

    Best regards,
    Dillion

    Monday, January 23, 2017 8:55 AM
  • User841411575 posted

    Thanks, going to give it a try.

    This is what I have: 

    DateTime now = DateTime.Now;
                            if (query.LastTime.Date > now.AddHours(-24) && query.LastTime <= now)
                            {
                                Response.Redirect("https://site.com");
                            }

    LastTime comes from the database. My LastTime was of last week. Today when I went to the site, I was not redirected. Even that it has been passed more than 24hours. 

    Any idea of what could be wrong? 

    Monday, February 6, 2017 3:14 PM
  • User281315223 posted

    I'd recommend just using the AddDays() method, as it would likely read much better :

    var query = ent.UsageTrackings.OrderByDescending(emp => emp.Time)
                                  .FirstOrDefault(emp => emp.Name == domain)
                         
    if (query != null)
    {
          var lastEntry = query.EntryTime;
          var oneDayAgo = DateTime.Now.AddDays(-1);
          if (lastEntry <= oneDayAgo)
          {
                Response.Redirect("http://google.com");
          }                       
    }

    Monday, February 6, 2017 3:51 PM