472,378 Members | 1,317 Online

# startdate / enddate sum

all,
I have a startdate and enddate, i wish to be able to calculate how
many weeks have been selected. so for example, using the startdate and
enddate, i select 11/02/2008 and 25/02/2008 i need a method to sum how
many weeks that is.
Any suggestions?Where do i start?

Cheers!
Feb 28 '08 #1
4 1857
DateTime start = new DateTime(2008,02,11), end = new
DateTime(2008,02,25);
double weeks = end.Subtract(start).TotalDays / 7;

You might want to use Math.Floor() or Math.Ceiling() to round up or down...

Marc
Feb 28 '08 #2
On 28 Feb, 09:39, "Marc Gravell" <marc.grav...@gmail.comwrote:
* * * * * * DateTime start = new DateTime(2008,02,11), end = new
DateTime(2008,02,25);
* * * * * * double weeks = end.Subtract(start).TotalDays / 7;

You might want to use Math.Floor() or Math.Ceiling() to round up or down....

Marc
thanks for your replies! great help.
Marc, could i do the following..
DateTime start = new DateTime(variable name), end = new
DateTime(variable name);
double weeks = end.Subtract(start).TotalDays / 7;
because those dates are selected at the users command. then using
Math.Floor() to round up?
Feb 28 '08 #3
It depends what [variable name] is...
If it is already a DateTime, then just use in place of [start] and [end]
If they are strings, then you'll need to use DateTime.Parse().

Alternatively, give the user a date-picker, and just use the .Value (which
should be a DateTime).

Marc
Feb 28 '08 #4
Finished code:

// Convert strings to DateTime
DateTime dtStart = Convert.ToDateTime(udt.strStartDate);
DateTime dtEnd = Convert.ToDateTime(udt.strEndDate);

// Subtracts End Date from Start Date
double weeks = dtEnd.Subtract(dtStart).TotalDays / 7;

// Rounds the value to the closest whole number int
double fvWeeks = Math.Floor((weeks + 0.5));

Regards to Jon Skeet & Marc Gravell
Feb 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.