Results 1 to 3 of 3

Thread: Amount of Weekdays in a period

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2008
    Location
    South Africa
    Posts
    395

    Amount of Weekdays in a period

    I need to find out how Weekdays there are between two days.

    That means..... Mon, Tue, Wed, Thur, Fri...

    how can i do this?
    I know i can not just divide the Amount of days by 5 (for the weeks) and then subtract (2 * Amount of weeks)' (for the weekend), because the first date may start on a Thursday...

    Can anybody help me.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Amount of Weekdays in a period

    1. Get a number of whole weeks between the two given dates, multiply by 5
    2. Add the number of workdays in the starting week and in the ending one.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Amount of Weekdays in a period

    try this:

    vb Code:
    1. Sub CountDayOfWeekA(ByVal fromDate As Date, ByVal toDate As Date)
    2.  
    3.     Dim theCounter As New Dictionary(Of DayOfWeek, Integer)
    4.     Dim d As Date = fromDate
    5.  
    6.     Dim ctr As Integer = 0
    7.  
    8.     Dim sb As New System.Text.StringBuilder
    9.     sb.Append("Your start date was a " & fromDate.DayOfWeek.ToString & ".")
    10.     sb.Append(ControlChars.CrLf & ControlChars.CrLf)
    11.  
    12.  
    13.     Do While d <= toDate
    14.         If theCounter.ContainsKey(d.DayOfWeek) Then
    15.             theCounter(d.DayOfWeek) += 1
    16.         Else
    17.             theCounter.Add(d.DayOfWeek, 1)
    18.         End If
    19.         d = d.AddDays(1)
    20.         ctr += 1
    21.     Loop
    22.  
    23.     For Each entry As KeyValuePair(Of DayOfWeek, Integer) In theCounter
    24.         sb.AppendFormat("{0} {1}s" & vbNewLine, entry.Value, entry.Key)
    25.     Next
    26.  
    27.     sb.Append(ControlChars.CrLf)
    28.     sb.Append("There are " & Format(ctr, "###,###,###,###,###") & " days between then and your end date.")
    29.     sb.Append(ControlChars.CrLf & ControlChars.CrLf)
    30.  
    31.     Dim elapsedHours As Long = ((ctr - 2) * 24) + (24 - DateAndTime.DateDiff(DateInterval.Hour, CType("00:00:00", Date), CType(dtp1.Value.TimeOfDay.ToString, Date))) + DateAndTime.DateDiff(DateInterval.Hour, CType("00:00:00", Date), CType(dtp3.Value.TimeOfDay.ToString, Date))
    32.  
    33.     sb.Append("or " & Format(elapsedHours, "###,###,###,###,###") & " hours (approx.)")
    34.     sb.Append(ControlChars.CrLf)
    35.  
    36.     Dim elapsedMinutes As Long = ((ctr - 2) * 1440) + (1440 - DateAndTime.DateDiff(DateInterval.Minute, CType("00:00:00", Date), CType(dtp1.Value.TimeOfDay.ToString, Date))) + DateAndTime.DateDiff(DateInterval.Minute, CType("00:00:00", Date), CType(dtp3.Value.TimeOfDay.ToString, Date))
    37.  
    38.     sb.Append("or " & Format(elapsedMinutes, "###,###,###,###,###") & " minutes (approx.)")
    39.     sb.Append(ControlChars.CrLf)
    40.  
    41.     Dim elapsedSeconds As Long = ((ctr - 2) * 86400) + (86400 - DateAndTime.DateDiff(DateInterval.Second, CType("00:00:00", Date), CType(dtp1.Value.TimeOfDay.ToString, Date))) + DateAndTime.DateDiff(DateInterval.Second, CType("00:00:00", Date), CType(dtp3.Value.TimeOfDay.ToString, Date))
    42.  
    43.     sb.Append("or " & Format(elapsedSeconds, "###,###,###,###,###") & " seconds (approx.)")
    44.     MessageBox.Show(sb.ToString, "Summary", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
    45.  
    46. End Sub

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