• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 97
  • Last Modified:

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

0
skij
Asked:
skij
  • 2
2 Solutions
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now