Get the number of months between two DateTimes - vb.net

mnth = DateDiff(DateInterval.Month, 8/30/2012, 10/1/2012)
gives mnth = 2. But when we look, there is only 32 days between these dates. I am expecting a result mnth=1 as there is only 32 days between these days.
Pls help..
In my scenario i can consider a 15+ days to be a month but if it is less than 15, it should'nt be considered.

To get the number of complete months you can do different things depending on your interpretation,
Public Function CompleteMonthsBetweenA( _
ByVal start As DateTime, _
ByVal end As DateTime) As Integer
Dim invertor = 1
If (start > end) Then
Dim tmp = end
end = start
start = tmp
invertor = -1
End If
Dim diff = ((end.Year - start.Year) * 12) + end.Month - start.Month
If start.Day > end.Day Then
Return (diff - 1) * invertor
Else
Return diff * invertor
End If
End Function
With this function the number of complete months between 31/05/2011 (dd/mm/yy) and 30/06/2011 is 0 but between 30/06/2011 and 31/07/2011 is 1. Which may or may not be what you expect.
Public Function CompleteMonthsBetweenB( _
ByVal start As DateTime, _
ByVal end As DateTime) As Integer
Dim invertor = 1
If (start > end) Then
Dim tmp = end
end = start
start = tmp
invertor = -1
End If
Dim diff = ((end.Year - start.Year) * 12) + end.Month - start.Month
Dim startDaysInMonth = DateTime.DaysInMonth(start.Year, start.Month)
Dim endDaysInMonth = DateTime.DaysInMonth(end.Year, end.Month)
If (start.Day / startDaysInMonth) > (end.Day / endDaysInMonth) Then
Return (diff - 1) * invertor
Else
Return diff * invertor
End If
End Function
With this function the ratio Day / DaysInMonth is taken so the relative completion of the two months can be assessed.
Public Function CompleteMonthsBetweenC( _
ByVal start As DateTime, _
ByVal enddate As DateTime) As Integer
Dim invertor = 1
If (start > enddate) Then
Dim tmp = enddate
enddate = start
start = tmp
invertor = -1
End If
Dim diff = ((enddate.Year - start.Year) * 12) + enddate.Month - start.Month
Dim remainingDays = _
(DateTime.DaysInMonth(start.Year, start.Month) - start.Day) + enddate.Day
If remainingDays < 15 Then
Return (diff - 1) * invertor
Else
Return diff * invertor
End If
End Function
This function only rounds down if the surplus days are less than the magic number 15, which I think is what you are asking for in your update.
Public Function CompleteMonthsBetweenD( _
ByVal start As DateTime, _
ByVal end As DateTime) As Integer
Return end.Subtract(start).TotalDays \ 30.436875
End Function
This function takes the simpler approach of dividing the total number of days by the average number of days per month in the Gregorian Calendar.

The difference in months is calculated without regard to the day component of the dates.
For example, the difference in months between 8/31/2012 and 9/1/2012 is 1, eventhough it's only one day between the dates.
If you want to consider the day component you have to get the date difference in days instead of months, and calculate how many months you want that to be.

This is the class I use (it's C# but really easy to convert to VB.NET).
It is usefull for years, months, days... it is ideal for displaying ages in #Y-#M-#D format.
public class DateDifference
{
/// <summary>
/// defining Number of days in month; index 0=> january and 11=> December
/// february contain either 28 or 29 days, that's why here value is -1
/// which wil be calculate later.
/// </summary>
private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/// <summary>
/// contain from date
/// </summary>
private DateTime fromDate;
/// <summary>
/// contain To Date
/// </summary>
private DateTime toDate;
/// <summary>
/// this three variable for output representation..
/// </summary>
private int year;
private int month;
private int day;
public DateDifference(DateTime d1, DateTime d2)
{
int increment;
if (d1 > d2)
{
this.fromDate = d2;
this.toDate = d1;
}
else
{
this.fromDate = d1;
this.toDate = d2;
}
///
/// Day Calculation
///
increment = 0;
if (this.fromDate.Day > this.toDate.Day)
{
increment = this.monthDay[this.fromDate.Month - 1];
}
/// if it is february month
/// if it's to day is less then from day
if (increment == -1)
{
if (DateTime.IsLeapYear(this.fromDate.Year))
{
// leap year february contain 29 days
increment = 29;
}
else
{
increment = 28;
}
}
if (increment != 0)
{
day = (this.toDate.Day + increment) - this.fromDate.Day;
increment = 1;
}
else
{
day = this.toDate.Day - this.fromDate.Day;
}
///
///month calculation
///
if ((this.fromDate.Month + increment) > this.toDate.Month)
{
this.month = (this.toDate.Month + 12) - (this.fromDate.Month + increment);
increment = 1;
}
else
{
this.month = (this.toDate.Month) - (this.fromDate.Month + increment);
increment = 0;
}
///
/// year calculation
///
this.year = this.toDate.Year - (this.fromDate.Year + increment);
}
public override string ToString()
{
//return base.ToString();
return this.year + " Year(s), " + this.month + " month(s), " + this.day + " day(s)";
}
public int Years
{
get
{
return this.year;
}
}
public int Months
{
get
{
return this.month;
}
}
public int Days
{
get
{
return this.day;
}
}
}
USAGE:
DateDifference diff = new DateDifference(date1, date2);
int months = (diff.Years*12) + diff.Months + diff.Days > 15 ? 1 : 0;

If you want the number of complete month, then you'll need to compare date that starts at the begining of the closet month.
Sub Main()
' mnth = DateDiff(DateInterval.Month, 8/30/2012, 10/1/2012)
Console.WriteLine(GetCompleteMonthCount(New DateTime(2012, 8, 30), New DateTime(2012, 10, 1)))
Console.ReadLine()
End Sub
Public Function GetCompleteMonthCount(ByVal d1 As DateTime, ByVal d2 As DateTime) As Integer
If d1.Day <> 1 Then
d1 = d1.AddMonths(1)
d1 = New DateTime(d1.Year, d1.Month, 1)
End If
If d2.Day <> 1 Then
d2 = New DateTime(d2.Year, d2.Month, 1)
End If
Return DateDiff(DateInterval.Month, d1, d2)
End Function

The solutions above are all very good but perhaps a little over-complicated, how about
Function wholeMonthsEd(d1, d2) As Integer
' determine the DateDiff function output
' which gives calendar month difference
initMonths = DateDiff("m", d1, d2)
' do calcs on the Day of the month to deduct/not a calendar month
If Day(d2) < Day(d1) Then
initMonths = initMonths - 1
End If
wholeMonths = initMonths
End Function

Related

vb.net find 4th Wednesday of month with visual basic

I am trying to find the fourth Wednesday of the current Month and Year when a form loads
I have converted numerous C# code to Visual Basic with no results
Would someone explain what is wrong with this code OR explain how to accomplish this in VB Code
Private Sub SurroundingSub()
Dim thisDate As Date = Today
tbMonth.Text = thisDate.ToString("MMMM")
tbYear.Text = thisDate.ToString("yyyy")
Dim month As Integer
month = tbMonth.Text
Dim year As Integer
year = tbYear.Text
Dim fourthWed As Date = New Date(year, month, 4)
tbFour.Text = fourthWed.ToLongDateString
While fourthWed.DayOfWeek <> DayOfWeek.Wednesday
fourthWed = fourthWed.AddDays(1)
tbFour.Text = fourthWed.ToShortDateString
End While
End Sub
This is a JavaFX statement that I am trying to implement in VB.Net
if(TorF.equals("F") && today.isAfter(fourthTUEofMONTH))
This sets the date
public void setDATES() throws IOException, SQLException{
today = LocalDate.now();
fourthTUEofMONTH = LocalDate.now();
fourthTUEofMONTH = fourthTUEofMONTH.with(TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.TUESDAY));
endMONTH = LocalDate.now();
endMONTH = endMONTH.with(TemporalAdjusters.lastDayOfMonth());
}
For a more general solution, with my function you can easily find the first, second, third, forth or fifth sunday, monday, tuesday, wednesday or whatever you want:
Public Function GetXDayOfWeek(Day As DateTime,
DayOfWeek As DayOfWeek,
Optional Index As Integer = 1) As DateTime
Dim First As New Date(Day.Year, Day.Month, 1)
Dim Diff As Integer = (DayOfWeek - First.DayOfWeek + 7) Mod 7
Return First.AddDays(Diff + (Index - 1) * 7)
End Function
So if you want to find the forth wednesday of the current month, use it like:
Dim DateToFind As DateTime = GetXDayOfWeek(Today, DayOfWeek.Wednesday, 4)
Your while statement will stop on the first Wednesday it finds, not the fourth. Keep track of the number of Wednesdays you encounter as you iterate and once you find the fourth then you can update tbFour.
Also as mentioned in the comments you'll want to start at the first day of the year.
Dim fourthWed As Date = New Date(year, month, 1)
Dim wednesdayCursor As Integer = 0
While wednesdayCursor < 4
If fourthWed.DayOfWeek = DayOfWeek.Wednesday Then
wednesdayCursor += 1
End If
fourthWed = fourthWed.AddDays(1)
End While
'Subtract one day because we added one on loop:
fbFour.Text = fourthWed.AddDays(-1).ToShortDateString()
You should make the function of getting the fourth Wednesday into a separate method, perhaps generalizing it for any day of the week, but just for the fourth Wednesday...
Module Module1
Function FourthWedOfMonth(dt As DateTime) As Integer
Dim currDate = New DateTime(dt.Year, dt.Month, 1)
Dim nWednesdays = 0
While nWednesdays < 4
If currDate.DayOfWeek = DayOfWeek.Wednesday Then
nWednesdays += 1
End If
currDate = currDate.AddDays(1)
End While
Return currDate.Day - 1
End Function
Sub Main()
For mo = 1 To 12
Console.Write(FourthWedOfMonth(New DateTime(2020, mo, 17)) & " ")
Next
Console.ReadLine()
End Sub
End Module
Outputs the correct days for 2020:
22 26 25 22 27 24 22 26 23 28 25 23
If you wanted the DateTime of the fourth Wednesday, you could
Function FourthWedOfMonth(dt As DateTime) As DateTime
Dim currDate = New DateTime(dt.Year, dt.Month, 1)
Dim nWednesdays = 0
While nWednesdays < 4
If currDate.DayOfWeek = DayOfWeek.Wednesday Then
nWednesdays += 1
End If
currDate = currDate.AddDays(1)
End While
Return New DateTime(dt.Year, dt.Month, currDate.Day - 1)
End Function
and then Console.WriteLine(FourthWedOfMonth(DateTime.Today).ToString("dd-MMM-yyyy")) would output "22-Jul-2020" (at the time of writing).
I'd find the fourth Wednesday of the current month this way:
Private Sub FourthWednesdayOfCurrentMonth()
Dim firstOfMonth As DateTime = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1))
Dim firstWednesday As DateTime = firstOfMonth.AddDays((7 + (DayOfWeek.Wednesday - firstOfMonth.DayOfWeek)) Mod 7)
Dim fourthWednesday As DateTime = firstWednesday.AddDays(21)
tbYear.Text = fourthWednesday.Year
tbMonth.Text = fourthWednesday.Month
tbFour.Text = fourthWednesday.Day
End Sub
Written generically for any day of the week, that would change to:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fourthWednesday As DateTime = FourthWeekDayOfCurrentMonth(DayOfWeek.Wednesday)
tbYear.Text = fourthWednesday.Year
tbMonth.Text = fourthWednesday.Month
tbFour.Text = fourthWednesday.Day
End Sub
Private Function FourthWeekDayOfCurrentMonth(ByVal WeekDay As DayOfWeek) As DateTime
Dim firstOfMonth As DateTime = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1))
Dim firstWeekday As DateTime = firstOfMonth.AddDays((7 + (WeekDay - firstOfMonth.DayOfWeek)) Mod 7)
Return firstWeekday.AddDays(21)
End Function

Week of month, month of year,year

I would like to check Week of month, month of year and year but I have a problem with week of month
here is code:
Public Class BMW
Public Shared Function GetWeekNumber() As Integer
Threading.Thread.Sleep(2000)
Dim span As TimeSpan = DateTime.Now - New Date(DateTime.Now.Year, 1, 1)
Return (CInt(span.TotalDays) \ 7) + 1
If span.TotalDays = 1 Then
SendKeys.Send("{1}")
MsgBox(span.TotalDays)
Else
MsgBox("chybi1")
end if
end function
Google is your friend:
Private Function GetWeekNumber() As Integer
Dim span As TimeSpan = DateTime.Now - New Date(DateTime.Now.Year, 1, 1)
Return (CInt(span.TotalDays) \ 7) + 1
End Function
I'm not even sure this is what you are looking for..
Public Shared Function GetWeekNumber(ByVal datum As Date) As Integer
Dim weeknumber = Math.Floor(datum.Day / 7 + +1)
SendKeys.Send(weeknumber)
Return weeknumber
End Function

Compute and Show the range between two dates

I need sample code:
Date_today and Expiration date
Situation:
If the license is expired last February 21, 2010, it will compute and show the range within the date_today which is February 21, 2014 until from expiration_date which is February 21, 2010.
Note: Output is the month(s) not the year(s).
Like this:
Date Today: February 21, 2014
Expiration Date: February 21, 2010
Output: 48 months
Thanks in advance.
UPDATE
Only month difference:
''' <summary>
''' Shows the month difference between two dates with custom string format.
''' </summary>
''' <param name="Date1">Indicates the first date to compare.</param>
''' <param name="Date2">Indicates the second date to compare.</param>
Private Function DateDifference(ByVal Date1 As DateTime,
ByVal Date2 As DateTime) As Integer
Dim MonthDiff As Integer
Do Until Date1 > Date2
Date1 = Date1.AddMonths(1)
MonthDiff += 1
Loop
Return MonthDiff - 1
End Function
Usage:
Dim MonthDiff As Integer = DateDifference(DateTime.Parse("01/03/2013 00:00:00"),
DateTime.Parse("09/04/2014 01:01:01"))
MsgBox(String.Format("Month Difference {0}", CStr(MonthDiff)))
ORIGINAL
I'm sure that you could customize this snippet to accomplish your needs.
It returns a custom string format with the differences.
Usage Example:
MsgBox(DateDifference(DateTime.Parse("01/03/2013 00:00:00"),
DateTime.Parse("09/04/2014 01:01:01"),
"{0} Year(s), {1} Month(s), {2} Week(s), {3} Day(s), {4} Hour(s), {5} Minute(s) and {6} Second(s)"
Here is:
' Date Difference
' ( By Elektro )
'
''' <summary>
''' Shows the difference between two dates with custom string format.
''' </summary>
''' <param name="Date1">Indicates the first date to compare.</param>
''' <param name="Date2">Indicates the second date to compare.</param>
''' <param name="StringFormat">
''' Indicates the string format to display the difference, where:
''' {0} = Years, {1} = Months, {2} = Weeks, {3} = Days, {4} = Hours, {5} = Minutes and {6} = Seconds</param>
''' <returns>System.String.</returns>
Private Function DateDifference(ByVal Date1 As DateTime,
ByVal Date2 As DateTime,
ByVal StringFormat As String) As String
Dim Time As TimeSpan
Dim YearDiff As Integer, MonthDiff As Integer, WeekDiff As Integer
Do Until Date1 > Date2
Date1 = Date1.AddMonths(1)
MonthDiff += 1
If MonthDiff = 12 Then
YearDiff += 1
MonthDiff = 0
End If
Loop
MonthDiff -= 1
Date1 = Date1.AddMonths(-1)
Time = (Date2 - Date1)
WeekDiff = (Time.Days \ 7)
Time = (Time - TimeSpan.FromDays(WeekDiff * 7))
Return String.Format(StringFormat, YearDiff, MonthDiff, WeekDiff, Time.Days, Time.Hours, Time.Minutes, Time.Seconds)
End Function
This code returns the difference in months: (Please note, this is the absolute value. To get the non-absoloute value remove the Math.Abs() function.)
Public Shared Function MonthDifference(lValue As DateTime, rValue As DateTime) As Integer
Return Math.Abs((lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year))
End Function
Usage:
Dim date1 As New Date(2010, 2, 21)
Dim date2 As Date = Date.Today
Dim differenceInMonths As Integer = MonthDifference(date2, date1)
Here is a more straight forward way, but it does not account for the months that dont have 30 days.
Dim date1 As New Date(2010, 2, 21)
Dim date2 As Date = Date.Today
Dim difference As TimeSpan = date2.Subtract(date1)
Dim differenceInMonths As Integer = difference.TotalDays / 30
If you need the answer in a different unit of measure like days, minutes, etc., modify the second method.

compute age from given birthdate

I have 2 comboboxes and 2 textboxes. My first combobox contains months in this format january, february, etc, and the other combobox contains numbers from 1 to 31. My first textbox is txtyear. Once the user input birth year to txtyear a variable BOD will be equals to this.
Dim BOD as string
BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text
The purpose of my last textbox is to handle the age of the user that will be computed when the cursor lost focus on txtyear.
Can anyone help how to compute the age.
There are really two questions here:
How to convert the string input into a DateTime object
How to calculate age once you have your data in the correct format.
I'll let you follow other's instructions for how use TryParseExtract which is definitely the correct way to go here.
When determining someone's age from their DOB, try using this:
Public Function GetCurrentAge(ByVal dob As Date) As Integer
Dim age As Integer
age = Today.Year - dob.Year
If (dob > Today.AddYears(-age)) Then age -= 1
Return age
End Function
It is the vb version of the top answers on Jeff Atwood's very popular question How do I calculate someone's age
I wrote a blogpost about calculating age from dob as well.
Here's a little different way using the year and month properties of the Date class:
Dim BOD as string
BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text
Dim dt As Date
If Date.TryParseExact(BOD, "MMMM-dd-yyyy", Nothing, Globalization.DateTimeStyles.None, dt) Then
Dim Age As New Date(Now.Subtract(dt).Ticks)
MsgBox(String.Format("Your age is : {0} Years and {1} Months", Age.Year - 1, Age.Month - 1))
Else
MsgBox("Birth Date is in wrong format")
End If
Here's a technique when you use Visual Studio 2012
VB.NET language
Private Sub dtpBOfD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpBOfD.ValueChanged
lblAge.Text = Age(dtpBOfD.Value)
End Sub
Public Shared Function Age(DOfB As Object) As String
If (Month(Date.Today) * 100) + Date.Today.Day >= (Month(DOfB) * 100) + DOfB.Day Then
Return DateDiff(DateInterval.Year, DOfB, Date.Today)
Else
Return DateDiff(DateInterval.Year, DOfB, Date.Today) - 1
End If
End Function
Use this function
Function String2Date(ByVal sDay As String, ByVal sMonth as String, ByVal sYear as String) As Date
StdDateString = sMonth & " " & sDay & ", " & sYear
End Function
And apply it ..
Dim dt1 as Date = String2Date(ComboBox2.Text,ComboBox1.Text,txtYear.Text).ToShortDateString
Dim dt2 as Date = Now.ToShortDateString
Dim dt3 as TimeSpan = (dt2 - dt1)
Dim diff as Double = dt3.Days
Dim sAge as String
sAge = Str(Int(diff / 365)) & " Year "
diff = diff Mod 365
sAge = sAge & Str(Int(diff / 30)) & " Month(s)"
diff = diff Mod 30
sAge = sAge & Str(diff) & " Day(s)"
txtAge.Text = sAge
for complete age information use this code in c#.`
public string calculateDays(int day, int Month, int year)
{
int Diffyear;
int DiffMonth;
int DiffDay;
int cuYear=DateTime.Now.Year;
int cuMonth=DateTime.Now.Month;
int cuDay=DateTime.Now.Day;
string Age;
Diffyear= cuYear-year;
DiffMonth=cuMonth-Month;
DiffDay=cuDay-day;
if ((DiffMonth) < 0)
{
Diffyear -= 1;
}
if ((DiffDay) < 0)
{
DiffMonth -= 1;
if ((cuMonth - 1) < 8)
{
if (((cuMonth - 1) % 2) == 0)
{
if ((cuMonth - 1) == 2)
if (cuYear % 4 == 0)
{
DiffDay = 29 + DiffDay;
}
else
{
DiffDay = 28 + DiffDay;
}
else
DiffDay = 30 + DiffDay;
}
else
DiffDay = 31 + DiffDay;
}
else
{
if (((cuMonth - 1) % 2) == 0)
{
DiffDay = 31 + DiffDay;
}
else
{
DiffDay = 30 + DiffDay;
}
}
}
if ((DiffMonth) < 0)
{
DiffMonth = DiffMonth+12;
}
if (Diffyear < 0)
{
Diffyear = Diffyear * (-1);
}
if ((DiffDay) < 0)
{
DiffDay = DiffDay * (-1);
}
Age = "Age: " + Diffyear.ToString() + " year, " + DiffMonth.ToString() + " months, " + DiffDay.ToString() + " days";
return Age;
}
`
Dim d1 As Date
Dim d2 As Date
d1 = Format(dob.Value, "yyyy/MM/dd"
d2 = Format(System.DateTime.Now, "yyyy/MM/dd")
d = DateDiff(DateInterval.Year, d1, d2)'d-1 provides accurate age

calculate date add, but only weekdays

I would like to calculate a new date simply by using the build-in dateadd function, but take into account that only weekdays should be counted (or 'business days' so to speak).
I have come up with this simple algorithm, which does not bother about holidays and such. I have tested this with some simple dates, but would like some input if this can be done in better ways.
This sample assumes a week with 5 business days, monday-friday, where first day of the week is monday. Dateformatting used here is d-m-yyyy, the sample calculates with a startdate of october 1, 2009.
Here is the simple form:
Dim d_StartDate As DateTime = "1-10-2009"
Dim i_NumberOfDays As Integer = 12
Dim i_CalculateNumberOfDays As Integer
If i_NumberOfDays > (5 - d_StartDate.DayOfWeek) Then
i_CalculateNumberOfDays = i_NumberOfDays
Else
i_CalculateNumberOfDays = i_NumberOfDays + (Int(((i_NumberOfDays + (7 - d_StartDate.DayOfWeek)) / 5)) * 2)
End If
MsgBox(DateAdd(DateInterval.Day, i_CalculateNumberOfDays, d_StartDate))
Which I try to explain with the following piece of code:
''' create variables to begin with
Dim d_StartDate as Date = "1-10-2009"
Dim i_NumberOfDays as Integer = 5
''' create var to store number of days to calculate with
Dim i_AddNumberOfDays as Integer
''' check if amount of businessdays to add exceeds the
''' amount of businessdays left in the week of the startdate
If i_NumberOfDays > (5 - d_StartDate.DayOfWeek) Then
''' start by substracting days in week with the current day,
''' to calculate the remainder of days left in the current week
i_AddNumberOfDays = 7 - d_StartDate.DayOfWeek
''' add the remainder of days in this week to the total
''' number of days we have to add to the date
i_AddNumberOfDays += i_NumberOfDays
''' divide by 5, because we need to know how many
''' business weeks we are dealing with
i_AddNumberOfDays = i_AddNumberOfDays / 5
''' multiply the integer of current business weeks by 2
''' those are the amount of days in the weekends we have
''' to add to the total
i_AddNumberOfDays = Int(i_AddNumberOfDays) * 2
''' add the number of days to the weekend days
i_AddNumberOfDays += i_NumberOfDays
Else
''' there are enough businessdays left in this week
''' to add the given amount of days
i_AddNumberOfDays = i_NumberOfDays
End If
''' this is the numberof dates to calculate with in DateAdd
dim d_CalculatedDate as Date
d_CalculatedDate = DateAdd(DateInterval.Day, i_AddNumberOfDays, d_StartDate)
Thanks in advance for your comments and input on this.
I used the .DayOfWeek function to check if it was a weekend. This does not include holiday implementation. It has been tested. I realize this question is old but the accepted answer didn't work. However, I did like how clean it was so I thought I'd update it and post. I did change the logic in the while loop.
Function AddBusinessDays(startDate As Date, numberOfDays As Integer) As Date
Dim newDate As Date = startDate
While numberOfDays > 0
newDate = newDate.AddDays(1)
If newDate.DayOfWeek() > 0 AndAlso newDate.DayOfWeek() < 6 Then '1-5 is Mon-Fri
numberOfDays -= 1
End If
End While
Return newDate
End Function
Public Shared Function AddBusinessDays(ByVal startDate As DateTime, _
ByVal businessDays As Integer) As DateTime
Dim di As Integer
Dim calendarDays As Integer
'''di: shift to Friday. If it's Sat or Sun don't shift'
di = (businessDays - Math.Max(0, (5 - startDate.DayOfWeek)))
''' Start = Friday -> add di/5 weeks -> end = Friday'
''' -> if the the remaining (<5 days) is > 0: add it + 2 days (Sat+Sun)'
''' -> shift back to initial day'
calendarDays = ((((di / 5) * 7) _
+ IIf(((di Mod 5) <> 0), (2 + (di Mod 5)), 0)) _
+ (5 - startDate.DayOfWeek))
Return startDate.AddDays(CDbl(calendarDays))
End Function
Your plan seems like it should work. Make sure you wrap it in a function instead of doing out the calculations every place you use it so that if/when you discover you need to account for holidays, you don't have to change it in tons of places.
The best way I can think of for implementing support for holidays would be to add days one at a time in a loop. Each iteration, check if its a weekend or a holiday, and if it is add another day and continue (to skip it). Below is an example in pseudocode (I don't know VB); no guarantees its correct. Of course, you need to provide your own implementations for isWeekend() and isHoliday().
function addBusinessDays(startDate, numDays)
{
Date newDate = startDate;
while (numDays > 0)
{
newDate.addDays(1);
if (newDate.isWeekend() || newDate.isHoliday())
continue;
numDays -= 1;
}
return newDate;
}
My first thought for the holiday thing was to simply look up the number of holidays between the start date and the end date and add that to your calculation, but of course this won't work because the end date is dependent on the number of holidays in that interval. I think an iterative solution is the best you'll get for holidays.
I'm using this code to calculate the date:
dayOfWeek = startDate.DayOfWeek
weekendDays = 2 * Math.Floor((dayOfWeek + businessDays - 0.1) / 5)
newDate = startDate.AddDays(businessDays + weekendDays)
The second line computes the number of full weekends that we have to add and then multiplies them by 2 to obtain the number of days.
The additional -0.1 constant is used to avoid adding days if (dayOfWeek + businessDays) is multiple of 5, and final date is friday.
Private Function AddBusinessDays(ByVal dtStartDate As DateTime, ByVal intVal As Integer) As DateTime
Dim dtTemp As DateTime = dtStartDate
dtTemp = dtStartDate.AddDays(intVal)
Select Case dtTemp.DayOfWeek
Case 0, 6
dtTemp = dtTemp.AddDays(2)
End Select
AddBusinessDays = dtTemp
End Function
please check this code for adding working days
Dim strDate As Date = DateTime.Now.Date
Dim afterAddDays As Date
Dim strResultDate As String
Dim n As Integer = 0
For i = 1 To 15
afterAddDays = strDate.AddDays(i)
If afterAddDays.DayOfWeek = DayOfWeek.Saturday Or afterAddDays.DayOfWeek = DayOfWeek.Sunday Then
n = n + 1
End If
Next
strResultDate = afterAddDays.AddDays(n).ToShortDateString()
Private Function AddXWorkingDays(noOfWorkingDaysToAdd As Integer) As Date
AddXWorkingDays = DateAdd(DateInterval.Weekday, noOfWorkingDaysToAdd + 2, Date.Today)
If Weekday(Today) + noOfWorkingDaysToAdd < 6 Then AddXWorkingDays = DateAdd(DateInterval.Weekday, 2, Date.Today)
End Function
One approach would be to iterate from the start date and add or subtract one day with each iteration if the date is not a Saturday or Sunday. If zero is passed as iAddDays then the function will return dDate even if that date is a Saturday or Sunday. You could play around with the logic to get the outcome you are looking for if that scenario is a possibility.
Public Function DateAddWeekDaysOnly(ByVal dDate As DateTime, ByVal iAddDays As Int32) As DateTime
If iAddDays <> 0 Then
Dim iIncrement As Int32 = If(iAddDays > 0, 1, -1)
Dim iCounter As Int32
Do
dDate = dDate.AddDays(iIncrement)
If dDate.DayOfWeek <> DayOfWeek.Saturday AndAlso dDate.DayOfWeek <> DayOfWeek.Sunday Then iCounter += iIncrement
Loop Until iCounter = iAddDays
End If
Return dDate
End Function
Easy way
function addWerktage($date,$tage){
for($t=0;$t<$tage;$t++){
$date = $date + (60*60*24);
if(date("w",$date) == 0 || date("w",$date) == 6){ $t--; }
}
return $date;
}
This produces the same result as the accepted answer, including starting on weekends, while handling negative offsets and without looping. It's written in c# but should work in any enviroment where numeric weekdays start at Sunday and end at Saturday, and where integer division rounds to 0.
public static DateTime AddWeekdays(DateTime date, int offset)
{
if (offset == 0)
{
return date;
}
// Used to calculate the number of weekend days skipped over
int weekends;
if (offset > 0)
{
if (date.DayOfWeek == DayOfWeek.Saturday)
{
// Monday is 1 weekday away, so it will take an extra day to reach the next weekend
int daysSinceMonday = -1;
// Add two weekends for every five days
int normalWeekends = (offset + daysSinceMonday) / 5 * 2;
// Add one for this Sunday
int partialWeekend = 1;
weekends = normalWeekends + partialWeekend;
}
else
{
// It will take this many fewer days to reach the next weekend.
// Note that this works for Sunday as well (offset -1, same as above)
int daysSinceMonday = date.DayOfWeek - DayOfWeek.Monday;
// Add two weekends for every five days (1 business week)
weekends = (offset + daysSinceMonday) / 5 * 2;
}
}
else
{
// Same as the positive offset case, but counting backwards.
// daysSinceFriday will be 0 or negative, except for Saturday, which is +1
int daysSinceFriday = date.DayOfWeek - DayOfWeek.Friday;
weekends = date.DayOfWeek == DayOfWeek.Sunday
? (offset + 1) / 5 * 2 - 1
: (offset + daysSinceFriday) / 5 * 2;
}
return date.AddDays(offset + weekends);
}
Since the pattern of 2 extra days per 5 days repeats after a full week, you can effectively exhaustively test it:
private static DateTime AddWeekdaysLooping(DateTime date, int offset)
{
DateTime newDate = date;
int step = Math.Sign(offset);
while (offset != 0)
{
newDate = newDate.AddDays(step);
if (newDate.DayOfWeek != DayOfWeek.Sunday && newDate.DayOfWeek != DayOfWeek.Saturday)
{
offset -= step;
}
}
return newDate;
}
void TestWeekdays()
{
DateTime initial = new DateTime(2001, 1, 1);
for (int day = 0; day < 7; day += 1)
{
for (int offset = -25; offset <= 25; offset += 1)
{
DateTime start = initial.AddDays(day);
DateTime expected = AddWeekdaysLooping(start, offset);
DateTime actual = AddWeekdays(start, offset);
if (expected != actual) {
throw new Exception($"{start.DayOfWeek} + {offset}: expected {expected:d}, but got {actual:d}");
}
}
}
}
Dim result As Date
result = DateAdd("d", 2, Today)
If result.DayOfWeek = DayOfWeek.Saturday Then
result = DateAdd("d", 2, result)
MsgBox(result)
ElseIf result.DayOfWeek = DayOfWeek.Sunday Then
result = DateAdd("d", 1, result)
MsgBox(result)
ElseIf result.DayOfWeek = DayOfWeek.Monday Then
MsgBox(result)
ElseIf result.DayOfWeek = DayOfWeek.Tuesday Then
MsgBox(result)
ElseIf result.DayOfWeek = DayOfWeek.Wednesday Then
MsgBox(result)
ElseIf result.DayOfWeek = DayOfWeek.Thursday Then
MsgBox(result)
ElseIf result.DayOfWeek = DayOfWeek.Friday Then
MsgBox(result)
End If