Android Question Get the week we're in by date

konradwalsh

Active Member
Licensed User
Longtime User
Hi All

I already know I am probably going to be flamed for this, but honestly, I have read the threads on each DateUtils and related threads..

I want to achieve this:
I want to store a kids worksheet by week in a DB
The week runs Monday to Sunday. So this week, I would like to return

Sep 12 - 18

Therefore I need to know Mondays date plus 6 days later date

I want to write to a database to store the resulting value as the key. This way the user can go to that week and call up his worksheet that he saved.


I am using the examples I found but they are not reliable or my implementation is not reliable.

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

B4X:
     Dim LastMonday As Long = GetPreviousDay(2)
     Main.WeekTick = LastMonday
    'You probably want to set the time of LastSunday to 23:59:59
     LastMonday = DateUtils.SetDateAndTime(DateTime.GetYear(LastMonday), DateTime.GetMonth(LastMonday), _
        DateTime.GetDayOfMonth(LastMonday), 23, 59, 59)
        DateTime.DateFormat="MMM dd"
        Dim thisMonth As String = DateTime.Date(LastMonday)
        LogColor("LAST MONDAY  MonthName  : " &   thisMonth,Colors.Green)
            DateTime.DateFormat=" dd"
        Dim dayNo As  Int = DateTime.Date(LastMonday)
        LogColor("LAST MONDAY  day  : " & dayNo,Colors.Green)
        LogColor("Week is : " & dayNo & "-" & (dayNo + 6),Colors.Green)
        Main.serviceWeek = thisMonth & "-" & (dayNo + 6)

        LogColor(Main.serviceWeek,Colors.Magenta)
         Dim LastSunday As Long = GetPreviousDay(1)
         Dim Monday As Long = DateTime.Add(LastSunday, 0, 0, -6)
           'You probably want to set the time of LastSunday to 23:59:59
           'LastSunday = DateUtils.SetDateAndTime(DateTime.GetYear(LastSunday),          DateTime.GetMonth(LastSunday), _
   '   DateTime.GetDayOfMonth(LastSunday), 23, 59, 59)
     

         Log("Monday: " & DateUtils.TicksToString(Monday))
         Log("Sunday: " & DateUtils.TicksToString(LastSunday))
    Catch
        LogColor(LastException,Colors.Green)
    End Try
End Sub


If I run this today.. I will get Sep 12-18
But if I run yesterday on a Monday, I will get a the dates for Saturday to Friday
 
Last edited:

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I don't think the DateUtils has a get week number function, but perhaps you could just get the standard week of the date and base your calculations on that and use DateUtils to window it:
B4X:
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume
    Dim plDate As Long = DateTime.Now
    Log("The week number for " & DateTime.Date(plDate) & " is week " & GetWeekNumber(plDate))
    Dim poPeriod As Period
    poPeriod.Initialize
    poPeriod.Days = -7
    plDate = DateUtils.AddPeriod(plDate, poPeriod)
    Log("The week number for " & DateTime.Date(plDate) & " is week " & GetWeekNumber(plDate))
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub GetWeekNumber(DateTicks As Long) As Int
    Dim psCurrFmt As String = DateTime.DateFormat
    DateTime.DateFormat = "w"
    Dim piCurrentWeek As Int = DateTime.Date(DateTicks)
    DateTime.DateFormat = psCurrFmt
    Return piCurrentWeek
End Sub
 
Upvote 0

konradwalsh

Active Member
Licensed User
Longtime User
OK Based on replies and reading up.. this seems more reliable

Seems calculating forward is more reliable than backwards

B4X:
 DateTime.DateFormat = "MMM - dd"
         Dim Sunday As Long = GetNextDay(1)
         Dim NextSunday As Long = DateTime.Add(Sunday, 0, 0, 7)
         Log("Next Sunday: " & DateUtils.TicksToString(NextSunday))
         Dim LastMonday As Long = DateTime.Add(NextSunday, 0, 0, -6)
         Log("Last Monday: " & DateUtils.TicksToString(LastMonday))

B4X:
'1 -> Sunday, 7-> Saturday
Sub GetNextDay(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



HOWEVER, I know this gets off topic a little.. the result I get is -

Next Sunday: Sep - 18 00:00:00
Last Monday: Sep - 12 00:00:00

despite having this
DateTime.DateFormat = "MMM - dd"
 
Upvote 0
Top