Results 1 to 3 of 3

Thread: [RESOLVED] [02/03] Months to Weeks

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] Months to Weeks

    vb.net Code:
    1. Dim startDate As Date = someDate
    2. Dim endDate As Date = startDate.AddMonths(months)
    3. Dim interval As TimeSpan = endDate.Subtract(startDate)
    4. Dim weeks As Integer = interval.Days \ 7
    5. Dim days As Integer = interval.Days Mod 7
    6.  
    7. MessageBox.Show(String.Format("There are {0} weeks and {1} days between {2:d} and {3:d}", _
    8.                               weeks, _
    9.                               days, _
    10.                               startDate, _
    11.                               endDate))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width