|
-
Aug 13th, 2007, 02:52 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [02/03] Months to Weeks
Hello again friends.
In my program, I have an initial startdate. Then, I need to add a certain number of months to that date (anything from 1 month to 36 months). That is done, but now, I must find a way to calculate how many weeks are present in this timeframe. So, If I add 2 months, I must know how many weeks there are in the 2 months, if I add 7 months, I need to know how many weeks are in those 7 months.
This is my very sad effort, to make this work:
Code:
Private Function suggMonthsToWeeks(ByVal Months As Integer) As Integer 'get number of weeks
Dim StartDate As Date 'frm dt picker
Dim EndDate As Date 'add months
Dim Weeks As Integer 'how many weeks
Dim AppWeekDay As Integer 'what day is it?
StartDate = dtSuggStartDate.Value 'store start date
EndDate = DateAdd(DateInterval.Month, Months, StartDate) 'add number of months
AppWeekDay = Weekday(EndDate) 'work out what day of week it is
Select Case AppWeekDay
Case 2 ' monday
EndDate = EndDate.AddDays(-3)
Case 3 'tue
EndDate = EndDate.AddDays(-4)
Case 4 'wed
EndDate = EndDate.AddDays(-5)
Case 5 'thu
EndDate = EndDate.AddDays(-6)
Case 6 'fri
EndDate = EndDate.AddDays(-7)
Case 7 'sat
EndDate = EndDate.AddDays(-7)
End Select
Weeks = DatePart(DateInterval.Weekday, EndDate) 'show number of weeks ??????
suggMonthsToWeeks = Weeks
End Function
Any advice friends?
-
Aug 13th, 2007, 04:55 AM
#2
Re: [02/03] Months to Weeks
vb.net Code:
Dim startDate As Date = someDate
Dim endDate As Date = startDate.AddMonths(months)
Dim interval As TimeSpan = endDate.Subtract(startDate)
Dim weeks As Integer = interval.Days \ 7
Dim days As Integer = interval.Days Mod 7
MessageBox.Show(String.Format("There are {0} weeks and {1} days between {2:d} and {3:d}", _
weeks, _
days, _
startDate, _
endDate))
-
Aug 13th, 2007, 05:18 AM
#3
Thread Starter
Hyperactive Member
Re: [02/03] Months to Weeks
Wow!! You make it look so easy!
Thank you very very much!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|