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

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 
Upvote 0
Top