Time Comparison

  • Thread starter Thread starter mc
  • Start date Start date
Hi,
How to do a time comparison?

You should look for:
DateTime.Compare(...)
DateTime.CompareTo(...)

eventually:
DateTime.Subtract(...)

HTH
Marcin
 
Hi,

Suppose I want my program to do something at 1pm everyday, how do I do the
comparison?

Thanks.
 
mc,

You don't use a DateTime to do it. This is a task for Scheduled Tasks.
You would write your program so that it performs the action once, and then
set up the Scheduled Tasks to run your program at 1 PM every day.

There is no need to run a program or a service which does nothing except
at a certain time to perform tasks.
 
mc,
As Nicholas suggests, this sounds like a scheduled task.

When I want to compare "times" without the date part I normally use
TimeSpan.Compare:

Something like:

TimeSpan now = DateTime.Now.TimeOfDay;

TimeSpan onePM = TimeSpan.FromHours(13);

if (TimeSpan.Compare(now, onePM) < 0)
DoSomething();

I created a TimeRange object (in VB.NET) that is a little more flexibly then
the above sample. For details see:

http://groups.google.com/groups?q=t...=#[email protected]&rnum=2

Hope this helps
Jay
 
Many ways. One from the top of my head.

Convert both datetimes to ticks, apply your formula to the ticks, create a
TimeSpan instance using formula ticks result as a parameter and call one of
the methods there to get what you want.
 
Many ways. One from the top of my head.

Convert both datetimes to ticks, apply your formula to the ticks, create a
TimeSpan instance using formula ticks result as a parameter and call one of
the methods there to get what you want.
 
eri presented the following explanation :
Does anyone know how to do Datetime comparison using c#?
Example:

A student did not send his/her work, the program will automatically send an
email reminder to student

From http://www.developmentnow.com/g/36_2004_11_0_0_17601/Time-Comparison.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

See the DateTime and TimeSpan structures, they support calculating with
dates.

But i suppose the real problem is elsewhere: *when* will the
application see that a student is late?
You will need some kind of routine that is called periodically. Either
by someone clicking a "check for due work" button or automatically by
some timer.

That routine then scans all uncompleted work assignments and compares
them to the duedate. If those assignments are stored in a database, use
a query that does that filtering for you instead of filtering in C#.

Hans Kesting
 
eri presented the following explanation :
Does anyone know how to do Datetime comparison using c#?
Example:

A student did not send his/her work, the program will automatically send an
email reminder to student

From http://www.developmentnow.com/g/36_2004_11_0_0_17601/Time-Comparison.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

See the DateTime and TimeSpan structures, they support calculating with
dates.

But i suppose the real problem is elsewhere: *when* will the
application see that a student is late?
You will need some kind of routine that is called periodically. Either
by someone clicking a "check for due work" button or automatically by
some timer.

That routine then scans all uncompleted work assignments and compares
them to the duedate. If those assignments are stored in a database, use
a query that does that filtering for you instead of filtering in C#.

Hans Kesting
 
Hans Kesting said:
eri presented the following explanation :

See the DateTime and TimeSpan structures, they support calculating with
dates.

But i suppose the real problem is elsewhere: *when* will the application
see that a student is late?
You will need some kind of routine that is called periodically. Either by
someone clicking a "check for due work" button or automatically by some
timer.

Well, polling isn't the best way. You can figure out which assignment is
due next and sleep until exactly its due time.
 
Hans Kesting said:
eri presented the following explanation :

See the DateTime and TimeSpan structures, they support calculating with
dates.

But i suppose the real problem is elsewhere: *when* will the application
see that a student is late?
You will need some kind of routine that is called periodically. Either by
someone clicking a "check for due work" button or automatically by some
timer.

Well, polling isn't the best way. You can figure out which assignment is
due next and sleep until exactly its due time.
 
Back
Top