iebidan Posted February 12, 2004 Posted February 12, 2004 Well, I'm trying to create a function to dispaly how many months - days have passed since a project started, I can know the days, but how can I know how many months???? Here is the code to get the days DateTime Uno = new DateTime(2004,01,01); DateTime Dos = new DateTime(2004,02,12); TimeSpan xx = Dos - Uno; double yy = xx.Days; this returns 42 days, but I need something that returns 1 month 12 days, is there a way to do this, or does anyone has any ideas??? Quote Fat kids are harder to kidnap
*Experts* Bucky Posted February 12, 2004 *Experts* Posted February 12, 2004 What determines what a "month" is? If the starting and ending dates were both in the middle of a month, there's no way to determine what "month" passed, if any at all. If "Uno" is always going to be the start of a month, then just subtract the difference in the months, add the difference of the years multiplied by twleve (to account for different years), then add in the days. So, in summary, with all the assumptions made above (pseudo-code): Months = Dos.Months - Uno.Months + (12 * (Dos.Year - Uno.Year)) Days = Dos.Days Or, if you want to assume that a "month" is always 30 days (which you've done here, even though January has 31 days), just do: Months = xx.Days \ 30 Days = xx.Days Mod 30 Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
iebidan Posted February 12, 2004 Author Posted February 12, 2004 Well, I think I'll have to create a whole function for this task, there is no simple way to do it, 'cause if the start day is not the same as the ending day you don't get an exact calc. I'll let u know how I solved this :D Quote Fat kids are harder to kidnap
TechnoTone Posted February 13, 2004 Posted February 13, 2004 Here's some VB.NET code that does the job: Dim StartDate As New DateTime(2004, 2, 13) Dim EndDate As DateTime = Date.Today Dim CountMonths As Integer Dim CountDays As Integer While StartDate.AddMonths(CountMonths + 1) <= EndDate CountMonths += 1 End While While StartDate.AddMonths(CountMonths).AddDays(CountDays + 1) <= EndDate CountDays += 1 End While MessageBox.Show(CountMonths.ToString & " months, " & CountDays.ToString & " days.") Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
iebidan Posted February 13, 2004 Author Posted February 13, 2004 PERFECT!!!! this code works great, now here is the C# version of it. THANK YOU TechnoTone. DateTime StartDate =new DateTime(2003, 1, 28); DateTime EndDate = new DateTime(2004,1,31); int CountMonths=0; int CountDays=0; while (StartDate.AddMonths(CountMonths +1) <= EndDate) { CountMonths +=1; } while (StartDate.AddMonths(CountMonths).AddDays(CountDays +1) <= EndDate) { CountDays +=1; } MessageBox.Show(CountMonths.ToString() + " months, " + CountDays.ToString() + " days."); Quote Fat kids are harder to kidnap
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.