Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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???

Fat kids are harder to kidnap
  • *Experts*
Posted

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

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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

Fat kids are harder to kidnap
Posted

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.")

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted

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.");

Fat kids are harder to kidnap

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...