Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date difference

Status
Not open for further replies.

Badgers

Programmer
Nov 20, 2001
187
US
Hi,

I am trying to calculate the number of interations between two dates and including a date interval.

For eg;

StartDate = 1/1/8
EndDate = 10/1/8
DateInterval 3

The idea to calculate the iterations is to create a percentage complete variable whilst looping.

Is there a function in C# which does this..?

Thanks


 
I'm not sure what you mean by DateInterval, or how you got to 3, but it sounds like it's just math. If that's the case you can code your own. The DateTime object has alot of built-in functionality to manipulate dates. DateTime is an immutable object, so you will always get a new object back. here is an example
Code:
var rightNow = DateTime.Now;
var today = rightNow.Date;
var tomorrow = today.AddDays(1);
In addition to the DateTime object there is the TimeSpan object. This may come in handy as well.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Hi

What I'm doing is hitting a web service with a start and end date, the date interval split the requests like:

Start 1/1/8 End 4/1/8 Interval 2 days

Process Run 1 1/1 to 3/1
Process Run 2 4/1

So then the web service is called repeatibily until the end date is hit, so it's this calculation so to create a percentage complete variable.

It's just the start/end date / interval doesn't always compute actual loops needed to call webservice due to even/odd dates.

Thats why wondering if there was a pre-built function to calculate such a thing...

Cheers

 
so interval is number of days between the 2 dates?
something like this should work.
Code:
public int DaysBetween(DateTime start, DateTime end)
{
   var timespan = start - end;
   return timespan.TotalDays;
}
if not this will. be careful of the +1 factor.
Code:
public int DaysBetween(DateTime start, DateTime end)
{
   var i = 0;
   var current = start;
   while(current <= end)
   {
       current = start.AddDays(i++);
   }
   return i;
}
the .net framework provides the building blocks for developers like us. Our applications are built atop these building blocks.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
You also need to consider that if your server is running on a different timezone, then there will be points in the day that you will get anomalous results.

You may wish to make sure that your server and application are runing the same locale settings, or make sure that all the dates are converted to a universal date time format to avoid odd situations..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top