Android Question Get all saturdays and sundays of a month

DonManfred

Expert
Licensed User
Longtime User
You need to write a small helper-sub to go over the given months and to count/list the mondays/sundays/whatever
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
You could try this As Manfred says: (comes from the DateUtils Class which can be found here)

DateUtils.GetDayOfWeekName(DateTime.Now)

Loop through that and If the result is equal to "Sunday" - save the date to a list and there you have it
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub GetDaysInMonth (Month As Int, Year As Int, RequiredDayOfWeek As Int) As List
   Dim res As List
   res.Initialize
   
   Dim firstday As Long = DateUtils.SetDate(Year, Month, 1)
   Dim firstdayInWeek As Int = DateTime.GetDayOfWeek(firstday)
   Dim p As Period
   p.Days = (7 + RequiredDayOfWeek - firstdayInWeek) Mod 7
   Dim GoodDay As Long = DateUtils.AddPeriod(firstday, p)
   p.Days = 7
   Do While DateTime.GetMonth(GoodDay) = Month
     res.Add(GoodDay)
     GoodDay = DateUtils.AddPeriod(GoodDay, p)
   Loop
   Return res
End Sub

Usage:
B4X:
Dim sundays As List = GetDaysInMonth(9, 2017, 1)
For Each s As Long In sundays
   Log($"$Date{s}"$)
Next
 
Upvote 0

Similar Threads

Top