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.
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
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: