Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have an issue calculate no of days between two dates

Now I have used the below code working fine which is exclude Saturday,Sunday

But real issues, special Holiday like Christmas/New Year and country National day or any other Govt holiday.

Please advice me

Maideen

What I have tried:

Public Shared Function GetBusinessDays(ByVal startD As DateTime, ByVal endD As DateTime) As Double
    Dim calcBusinessDays As Double = 1 + ((endD - startD).TotalDays * 5 - (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7
    If endD.DayOfWeek = DayOfWeek.Saturday Then calcBusinessDays -= 1
    If startD.DayOfWeek = DayOfWeek.Sunday Then calcBusinessDays -= 1
    Return calcBusinessDays
End Function
Posted
Updated 16-Jan-19 22:00pm

Did you try Google Search? Here is an example: c# calc business days[^]

Which found this: c# - Calculate the number of business days between two dates? - Stack Overflow[^]
 
Share this answer
 
Well, there's no "common list of holidays". It depends on country (national, public holidays) and religion. See: National day - Wikipedia[^].


The easiest way to detect if current day is a holiday, is to define a list of them. Note that some holidays are dependent of Easter...
VB.NET
Sub Main

	Dim startDate As DateTime = New DateTime(2019,1,1)
	Dim ThisYear As List(Of KeyValuePair(Of DateTime, Boolean)) = Enumerable.Range(0, 365) _
		.Select(Function(x) New KeyValuePair(Of DateTime, Boolean)(startDate.AddDays(x), IsHoliday(startDate.AddDays(x)))) _
		.ToList()

	'list is ready to use...
	
End Sub

' Define other methods and classes here
Function Easter(lyear As Long) As DateTime
	Dim C As Long, N As Long, K As Long, i As Long, J As Long, L As Long, M As Long, D As Long
	
	C = lyear \ 100
	N = lyear - 19 * (lyear \ 19)
	K = (C - 17) \ 25
	i = C - C \ 4 - (C - K) \ 3 + 19 * N + 15
	i = i - 30 * (i \ 30)
	i = i - (i \ 28) * (1 - (i \ 28) * (29 \ (i + 1)) * ((21 - N) \ 11))
	J = lyear + lyear \ 4 + i + 2 - C + C \ 4
	J = J - 7 * (J \ 7)
	L = i - J
	M = 3 + (L + 40) \ 44
	D = L + 28 - 31 * (M \ 4)

    Return New DateTime(lyear, M, D)

End Function

Function IsHoliday(ByVal thisday As DateTime) As Boolean

	Dim holidays As List(Of DateTime) = New List(Of DateTime)
	holidays.Add(New DateTime(thisday.Year, 1, 1)) 'New Year
	holidays.Add(New DateTime(thisday.Year, 1, 6)) 'Epiphany holiday 
	holidays.Add(Easter(thisday.Year)) 'Easter
	holidays.Add(Easter(thisday.Year).AddDays(1)) 'Easter Monday
	holidays.Add(Easter(thisday.Year).AddDays(49)) 'Whit Monday (48 days after Easter)
	holidays.Add(New DateTime(thisday.Year, 5, 1)) '1 May Labour Day
	holidays.Add(New DateTime(thisday.Year, 5, 3)) 'Constitution of 3th May - polish National Holiday
	holidays.Add(Easter(thisday.Year).AddDays(60)) 'Corpus Christi
	holidays.Add(New DateTime(thisday.Year, 8, 15)) 'Assumption of Mary
	holidays.Add(New DateTime(thisday.Year, 11, 1)) 'All Saints Day
	holidays.Add(New DateTime(thisday.Year, 12, 25)) 'Christmas Day
	holidays.Add(New DateTime(thisday.Year, 12, 26)) 'a second day of Christmas
	
	Return holidays.Contains(thisday)

End Function


Note: Easter function looks ugly, but works pefect!
 
Share this answer
 
Comments
Richard Deeming 18-Jan-19 10:34am    
NB: The Easter function works for "Western" Easter, but not for "Eastern" Easter, which is based on the Julian calendar. :)
https://en.wikipedia.org/wiki/Easter#Date[^]
Maciej Los 18-Jan-19 11:10am    
Good point!
I grew up in Western culture, so...
A'm i justified? :smile:
Richard Deeming 18-Jan-19 11:13am    
I think you're OK - the OP hasn't yet thought about countries where the weekend is Friday + Saturday instead of Saturday + Sunday. :)
Maciej Los 18-Jan-19 11:16am    
:laugh:
Thank you, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900