Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Sunday 31st March 2024 11:31am
That works for me.
THE community for Gambas users
https://forum.gambas.one/
Dim iDay As Integer = WeekDay(Now)
If iDay = 6 Then iDay = -1 ' Move sat to begining of week
Print Date(DateAdd(Now, gb.Day, -(iDay + 1) - gb.Week))
Woof! Woof! Sorry, but it returns the wrong Sunday. Using today's date, it returns the Sunday 24th March and not the 31st.grayghost4 wrote: ↑Tuesday 2nd April 2024 2:42pm Like a dog with a bone ... I can't let go until I have finished it
Dim iDay As Integer = WeekDay(Now) If iDay = 6 Then iDay = -1 ' Move sat to begining of week Print Date(DateAdd(Now, gb.Day, -(iDay + 1) - gb.Week))
Public Sub Form_Open()
Print GetLastSunday(Now)
Print GetLastSunday(Date(2024, 4, 1))
Print GetLastSunday(Date(2024, 3, 31))
Print GetLastSunday(Date(2024, 3, 25))
Print GetLastSunday(Date(2024, 3, 23))
End
Public Sub GetLastSunday(dDate As Date) As String
Dim sDay As Integer = WeekDay(dDate)
If sDay = 0 Then sDay = 7
Return Format(DateAdd(dDate, gb.Day, -sDay), "dddd d mmmm yyyy")
End
Mabe I did not understand what he neededas I need to get the Sunday to Monday dates for a report that run for the previous week (no matter what day the report would be run this week it would always report back the previous Monday to Sunday dates)
Dim iDay As Integer = WeekDay(Now)
If iDay = 6 Then iDay = -1 ' Move sat to begining of week
Print Date(DateAdd(Now, gb.Day, -(iDay) - gb.Week))
Print Date(DateAdd(Now, gb.Day, -iDay))
I think he wants a few things, as soon as i solved one issue he asked for another variant.grayghost4 wrote: ↑Wednesday 3rd April 2024 2:17pm I read the original post as wanting the sunday of the last full week to print the last week activity report.
Mabe I did not understand what he neededas I need to get the Sunday to Monday dates for a report that run for the previous week (no matter what day the report would be run this week it would always report back the previous Monday to Sunday dates)
for me it was a good exersize and learing how to use some of the date functions
rereading the OP i guess he wants the Monday of the last full week ... so just remove the "-1" from the iDay and print the following sunday
Dim iDay As Integer = WeekDay(Now) If iDay = 6 Then iDay = -1 ' Move sat to begining of week Print Date(DateAdd(Now, gb.Day, -(iDay) - gb.Week)) Print Date(DateAdd(Now, gb.Day, -iDay))
BruceSteers wrote: ↑Wednesday 3rd April 2024 3:42pmI think he wants a few things, as soon as i solved one issue he asked for another variant.grayghost4 wrote: ↑Wednesday 3rd April 2024 2:17pm I read the original post as wanting the sunday of the last full week to print the last week activity report.
Mabe I did not understand what he neededas I need to get the Sunday to Monday dates for a report that run for the previous week (no matter what day the report would be run this week it would always report back the previous Monday to Sunday dates)
for me it was a good exersize and learing how to use some of the date functions
rereading the OP i guess he wants the Monday of the last full week ... so just remove the "-1" from the iDay and print the following sunday
Dim iDay As Integer = WeekDay(Now) If iDay = 6 Then iDay = -1 ' Move sat to begining of week Print Date(DateAdd(Now, gb.Day, -(iDay) - gb.Week)) Print Date(DateAdd(Now, gb.Day, -iDay))
I'm pretty sure he's been given enough help/information needed to make the function/functions work how he needs it to now.
I think his biggest issue at first was he was adding integer values directly to date objects and that's a no no that's bound to cause problems
Now he knows to use DateAdd instead and has been shown a few tricks on how to get from one day to another.
I'm pretty sure he's now well equipped to figure out all he needs to