tags:

views:

51

answers:

2

Hello All,

I am using C# to send an email with an encrypted link. The encrypted portion of the link contains a time stamp that needs to be used to verify if the link is more than 48 hours old. How do I compare an old time to the current time and find out if the old time is more than 48 hours ago?

This is what I have now:

var hours = DateTime.Now.Ticks - data.DTM.Value.Ticks; //data.DTM = stored time stamp

        if (hours.CompareTo(48) > 1) //if link is more than 48 hours old, deny access.

            return View("LinkExpired");
        }

Comparing ticks seems like it's a very backwards way to do it and I know that the hours.CompareTo would have to be adjusted if I stick with comparing ticks. How can I just get a value for the number of hours that have passed?

+4  A: 
(DateTime.Now - data.DTM.Value).TotalHours > 48
Andrey
Amazing. I should have known it was something that simple. Thanks Andrey.
Jacob Huggart
btw this is interesting construct. DateTime overrides operator - and result is object of class TimeSpan. These things make me love c# (and hate java)
Andrey
+1  A: 

DateTime.Now.Ticks are not hours... they're ticks... very very small intervals in your computer.

try

    if ((DateTime.Now - data.DTM).TotalHours > 48) //if link is more than 48 hours old, deny access.

        return View("LinkExpired");
    }
Eoin Campbell