ASP.NET/C#: Test length between two times

Server.Time and PingPrevious.ClosingTime both return dates in this format:
07/02/2014 9:00:00 PM

How can I test if TradePrevious.ClosingTime is at least half an hour older than Server.Time?

This does NOT work:
if(Server.Time > (TradePrevious.ClosingTime + 1800)) Print("Previously pinged more than half an hour ago.");

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What are the data types of Server.Time and TradePrevious.Closingtime?
0
Fernando SotoRetiredCommented:
Hi  skij;

This should give you what you want assuming the two dates are DateTime objects.
TimeSpan diff = TradePrevious.ClosingTime.TimeOfDay - ServerTime.TimeOfDay;
if( diff >= .5 ) 
{
    // TradePrevious.ClosingTime is at least half an hour older than Server.Time?
}
else
{
    TradePrevious.ClosingTime is less then a half an hour older than Server.Time?
}

Open in new window

0
Fernando SotoRetiredCommented:
If they are strings then convert them to DateTime objects first.

DateTime ServerTime = DateTime.Parse("07/02/2014 9:00:00 PM");
DateTime TradePreviousClosingTime = DateTime.Parse("07/02/2014 10:15:00 PM");

Then you can use ServerTime in place of Server.Time and TradePreviousClosingTime in place of TradePrevious.ClosingTime in my above post.
0
Gustav BrockCIOCommented:
You should use TimeSpan for this:
TimeSpan timeDiff = Server.Time - TradePrevious.ClosingTime;
if (timeDiff.ToSeconds > 1800) Print("Previously pinged more than half an hour ago.");

Open in new window

/gustav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.