Android Question how to get last sunday date sring in "yyyymmdd" format ?

hears

Active Member
Licensed User
Longtime User
how to get last sunday date sring in "yyyymmdd" format ?

for change format i now use this code, but don't know how to get last SUNDAY

B4X:
DateTime.DateFormat = "yyyyMMdd"
    date1=DateTime.Date(LASTSUNDAY)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Log(DateUtils.TicksToString(LastSunday))


Sub LastSunday As Long
   Dim d As Int = DateTime.GetDayOfWeek(DateTime.Now)
   Dim t As Long = DateTime.Add(DateTime.Now, 0, 0, -(d-1))
   Return DateUtils.SetDate(DateTime.GetYear(t), DateTime.GetMonth(t), DateTime.GetDayOfMonth(t))
End Sub
Add a reference to DateUtils library.
 
Upvote 0

hears

Active Member
Licensed User
Longtime User
THANK YOU ,IT IS WORK
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Sub LastSunday As Long
Dim d As Int = DateTime.GetDayOfWeek(DateTime.Now)
Dim t As Long = DateTime.Add(DateTime.Now, 0, 0, -(d-1))
Return DateUtils.SetDate(DateTime.GetYear(t), DateTime.GetMonth(t), DateTime.GetDayOfMonth(t))
End Sub
how to get last sunday date sring in "yyyymmdd" format ?
If today's day is Sunday, your code returns today's date not last Sunday. in order to get last Sunday's date, your code should be like this:
B4X:
Sub LastSunday As Long
    Dim d As Int = DateTime.GetDayOfWeek(DateTime.Now)
    If d=1 Then
        Dim t As Long = DateTime.Add(DateTime.Now, 0, 0, -(d-1) -7)
    Else
        Dim t As Long = DateTime.Add(DateTime.Now, 0, 0, -(d-1))
    End If
    Return DateUtils.SetDate(DateTime.GetYear(t), DateTime.GetMonth(t), DateTime.GetDayOfMonth(t))
End Sub
 
Upvote 0

hears

Active Member
Licensed User
Longtime User
If today's day is Sunday, your code returns today's date not last Sunday. in order to get last Sunday's date, your code should be like this:
B4X:
Sub LastSunday As Long
    Dim d As Int = DateTime.GetDayOfWeek(DateTime.Now)
    If d=1 Then
        Dim t As Long = DateTime.Add(DateTime.Now, 0, 0, -(d-1) -7)
    Else
        Dim t As Long = DateTime.Add(DateTime.Now, 0, 0, -(d-1))
    End If
    Return DateUtils.SetDate(DateTime.GetYear(t), DateTime.GetMonth(t), DateTime.GetDayOfMonth(t))
End Sub
thanks
 
Upvote 0
Top