Solved

How do I calculate due date with whole months?

Posted on 2009-05-07
4
589 Views
Last Modified: 2013-12-17
I'm trying to calculate the due date of an invoice, and I have a date of invoice, days to due date (100) and I need to calculate due date with whole months:

For example:
if date of invoice = 07/05/2009 and days of due date = 120, then the due date calculated = 07/09/2009 (4 months exactly -whole months-).
My code:
 

DateTime invoiceDate=System.DateTime.Now();

int numberOfDays=100;

int numberOfMonths;

int restOfNumberOfDays;

Date dueDate;
 

if(wholeMonths)

{

    numberOfMonths = numberOfDays % 30;

    dueDate = dt.AddMonths(numberOfMonths);

    restOfNumberOfDays = numberOfDays - (numberOfMonths * 30);

    dueDate = dueDate.AddDays(restOfNumberOfDays);

}
 

This code would be correct if the month had 30 days long.

Open in new window

0
Comment
Question by:ReaL10
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
CuteBug earned 300 total points
ID: 24324354
There is a DateDiff method in Visual Basic which returns the value in months.

Just go through this site for details
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=52580

The decompiled code for DateDiff is provided there...

you need to convert it to C#.
Here is the code...
Public Shared Function DateDiff(ByVal Interval As DateInterval, ByVal Date1 As DateTime, ByVal Date2 As DateTime, ByVal Optional DayOfWeek As FirstDayOfWeek = 1, ByVal Optional WeekOfYear As FirstWeekOfYear = 1) As Long

Dim currentCalendar As Calendar

Dim span As TimeSpan = Date2.Subtract(Date1)

Select Case Interval

Case DateInterval.Year

currentCalendar = DateAndTime.CurrentCalendar

Return CLng((currentCalendar.GetYear(Date2) - currentCalendar.GetYear(Date1)))

Case DateInterval.Quarter

currentCalendar = DateAndTime.CurrentCalendar

Return CLng(((((currentCalendar.GetYear(Date2) - currentCalendar.GetYear(Date1)) * 4) + ((currentCalendar.GetMonth(Date2) - 1) / 3)) - ((currentCalendar.GetMonth(Date1) - 1) / 3)))

Case DateInterval.Month

currentCalendar = DateAndTime.CurrentCalendar

Return CLng(((((currentCalendar.GetYear(Date2) - currentCalendar.GetYear(Date1)) * 12) + currentCalendar.GetMonth(Date2)) - currentCalendar.GetMonth(Date1)))

Case DateInterval.DayOfYear, DateInterval.Day

Return CLng(Math.Round(Conversion.Fix(span.TotalDays)))

Case DateInterval.WeekOfYear

Date1 = Date1.AddDays(CDbl((0 - DateAndTime.GetDayOfWeek(Date1, DayOfWeek))))

Date2 = Date2.AddDays(CDbl((0 - DateAndTime.GetDayOfWeek(Date2, DayOfWeek))))

Return (CLng(Math.Round(Conversion.Fix(Date2.Subtract(Date1).TotalDays))) / 7)

Case DateInterval.Weekday

Return (CLng(Math.Round(Conversion.Fix(span.TotalDays))) / 7)

Case DateInterval.Hour

Return CLng(Math.Round(Conversion.Fix(span.TotalHours)))

Case DateInterval.Minute

Return CLng(Math.Round(Conversion.Fix(span.TotalMinutes)))

Case DateInterval.Second

Return CLng(Math.Round(Conversion.Fix(span.TotalSeconds)))

End Select

Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", New String() { "Interval" }))

End Function 

Open in new window

0
 
LVL 16

Assisted Solution

by:CuteBug
CuteBug earned 300 total points
ID: 24324366
I think this is what they are doing for difference in months

private static int monthDifference(DateTime startDate, DateTime endDate)

{

int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;

return Math.Abs(monthsApart);

}

Open in new window

0
 
LVL 4

Assisted Solution

by:cauos
cauos earned 100 total points
ID: 24324389
there is an already method to add days to datetime objct in .Net, all what you have to do is add number of days to the method AddDays()

DateTime duedate = DateTime.Now
duedate = duedate.AddDays(120)

0
 
LVL 9

Assisted Solution

by:tculler
tculler earned 100 total points
ID: 24325458
Well, if you want every month to be exactly 30 days, you could use the following method:
System.DateTime CalculateDueDate(System.Int32 myDaysUntilDue)

{

     return System.DateTime.Now.AddMonths(myDaysUntilDue / 30);

}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 27
Trouble with References... 5 22
Code works but how can I download the file? 20 45
Image(7) 1 33
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now