Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,892 questions

51,823 answers

573 users

How to calculate the number of weekdays between two dates in VB.NET

1 Answer

0 votes
Imports System

Public Class WeekdayCalculatorBetweenTwoDates
    Public Shared Sub Main()
        Console.WriteLine(countWeekDays(New DateTime(2025, 1, 1), New DateTime(2025, 1, 7)))
        Console.WriteLine(countWeekDays(New DateTime(2025, 2, 1), New DateTime(2025, 2, 10)))
        Console.WriteLine(countWeekDays(New DateTime(2024, 1, 1), New DateTime(2024, 12, 31)))
        Console.WriteLine(countWeekDays(New DateTime(2025, 1, 1), New DateTime(2025, 12, 31)))
    End Sub

    Public Shared Function countWeekDays(ByVal d1 As DateTime, ByVal d2 As DateTime) As Integer
        Dim ndays As Integer = 1 + Convert.ToInt32((d2 - d1).TotalDays)
        Dim nsaturdays As Integer = (ndays + Convert.ToInt32(d1.DayOfWeek)) / 7
		
        Return ndays - 2 * nsaturdays - (If(d1.DayOfWeek = DayOfWeek.Sunday, 1, 0)) + (If(d2.DayOfWeek = DayOfWeek.Saturday, 1, 0))
    End Function
End Class



' run:
'
' 5
' 6
' 262
' 259
'

 



answered Feb 18, 2025 by avibootz

Related questions

1 answer 69 views
1 answer 64 views
1 answer 79 views
1 answer 76 views
1 answer 57 views
1 answer 63 views
...