Android Question [Solved] Get dates for next week

asales

Expert
Licensed User
Longtime User
I use this function to get the previous day in a week:
B4X:
'1 -> Sunday, 7-> Saturday
Sub GetPreviousDay(DayOfWeek As Int) As Long
   Dim day As Int = DateTime.GetDayOfWeek(DateTime.Now)
   If day <> DayOfWeek Then
      day = (day + 7 - DayOfWeek) Mod 7
   End If
   Dim d As Long = DateTime.Add(DateTime.Now, 0, 0, -day)
   Return DateUtils.SetDate(DateTime.GetYear(d), DateTime.GetMonth(d), DateTime.GetDayOfMonth(d))
End Sub

https://www.b4x.com/android/forum/threads/get-dates-for-last-week.28020/#post-162399
I tried to change the code to get the dates in next week (passing the parameter DayOfWeek = 1 -> Sunday, 7-> Saturday), but without success.

Any help to create this function are welcome. Thanks.
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Hi, please try this:

B4X:
Sub GetNextDay(DayOfWeek As Int) As Long
       
    Dim day As Int = DateTime.GetDayOfWeek(DateTime.Now)
    day = (7-day+DayOfWeek) 
    Dim d As Long = DateTime.Add(DateTime.Now, 0, 0, day)
    Return d
    ' Return DateUtils.SetDate(DateTime.GetYear(d), DateTime.GetMonth(d), DateTime.GetDayOfMonth(d))
        
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub GetPreviousDay(DayOfWeek As Int) As Long
   Dim day As Int = DateTime.GetDayOfWeek(DateTime.Now)
   If day <= DayOfWeek Then DayOfWeek = DayOfWeek - 7
   Dim d As Long = DateTime.Add(DateTime.Now, 0, 0, DayOfWeek - day)
   Return DateUtils.SetDate(DateTime.GetYear(d), DateTime.GetMonth(d), DateTime.GetDayOfMonth(d))
End Sub

Sub GetNextDay(DayOfWeek As Int) As Long
   Dim day As Int = DateTime.GetDayOfWeek(DateTime.Now)
   If day >= DayOfWeek Then DayOfWeek = DayOfWeek + 7
   Dim d As Long = DateTime.Add(DateTime.Now, 0, 0, DayOfWeek - day)
   Return DateUtils.SetDate(DateTime.GetYear(d), DateTime.GetMonth(d), DateTime.GetDayOfMonth(d))
End Sub
 
Upvote 0
Top