469,601 Members | 2,327 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

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 1690
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by David Gray | last post: by
1 post views Thread by Ray S via .NET 247 | last post: by
1 post views Thread by Jeremy Ames | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.