Android Question [Solved] How to get the first Saturday in a month

Erel

Administrator
Staff member
Licensed User
B4X:
Dim t As Long = DateTime.DateParse("02/24/2015")
Dim sat As Long = FirstDayOfMonth(7, DateTime.GetMonth(t), DateTime.GetYear(t)) '7 for saturday
Log(DateTime.Date(sat))

Sub FirstDayOfMonth(Day As Int, Month As Int, Year As Int) As Long
   Dim s As Long = DateUtils.SetDate(Year, Month, 1)
   Dim d As Int = DateTime.GetDayOfWeek(s)
   Dim p As Period
   p.Initialize
   p.Days = (Day + 7 - d) Mod 7
   Return DateUtils.AddPeriod(s, p)
End Sub
I only tested it with the values in your question. Make sure to test it more.
 
Top