Find how many days remaining

RIAS
RIAS used Ask the Experts™
on
Hello,

How can I find a count for many days remaining for a Sunday  in that week .
Example:
Date : 24/09/2019 which is Tuesday
The date on Sunday in that week  will be 29/09/2019
So the count is 5

I will be provided with the current date, need to find the remaining days count till Sunday.

Regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HuaMin ChenProblem resolver

Commented:
Try
select case substring(datename(dw,getdate()),1,3) when 'Sat' then 1 when 'Fri' then 2 when 'Thu' then 3 when 'Wed' then 4 when 'Tue' then 5 when 'Mon' then 6 when 'Sun' then 0 end
go

Open in new window

AndyAinscowFreelance programmer / Consultant

Commented:
Logic:

daysremaining = 0
while not dategiven.DayOfWeek = sunday
  daysremaiing = daysremaining+1
  dategiven = dategiven+1 (day)
end while
Commented:
Try this Visual Basic code
  Dim countDays As Integer
  countDays = GetCountDaysForSunday(DateTime.Now)

.............................

 Public Function GetCountDaysForSunday(ByVal currentDate As DateTime) As Integer
        If (currentDate.DayOfWeek = DayOfWeek.Sunday) Then
            Return 0
        Else
            Return (7 - Int(currentDate.DayOfWeek))
        End If
    End Function

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
Hi Experts,

Thanks for the help.
Misha, thanks will try the code and brb
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
In its core, a single code line will do:

Function DaysUntilSunday(ByVal SomeDate) As Integer

	Const DaysInWeek As Integer = 7
	Dim Days As Integer
	
	Days = (DaysInWeek + DayOfWeek.Sunday - SomeDate.DayOfWeek) Mod DaysInWeek
	
	DaysUntilSunday = Days
	
End Function

Open in new window

Call it like this:

Sub Main
	
	Dim SomeDate As DateTime = New Datetime(2019, 09, 24)

	Console.WriteLine("Days until Sunday: {0}", DaysUntilSunday(SomeDate))

End Sub

' Output:
' Days until Sunday: 5

Open in new window

Author

Commented:
Thank you all.
Gustav Brock your code is spot on!!!! Thanks!!
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You are welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial