[Solved] Get the Previous Sunday from a Date
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: [Solved - sort of] Get the Previous Sunday from a Date
That works for me.
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: [Solved - sort of] Get the Previous Sunday from a Date
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))
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: [Solved - sort of] Get the Previous Sunday from a Date
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))
Here's my effort.
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
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: [Solved - sort of] Get the Previous Sunday from a Date
I read the original post as wanting the sunday of the last full week to print the last week activity report.
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
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
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: [Solved - sort of] Get the Previous Sunday from a Date
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))
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
If at first you don't succeed , try doing something differently.
BruceS
BruceS
-
- Posts: 363
- Joined: Wednesday 2nd December 2020 12:11am
- Location: Northampton, England
- Contact:
Re: [Solved - sort of] Get the Previous Sunday from a Date
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
He is WELL equipped to sort the rest of the issues out thank you to EVERYONE who has posted a example the different examples on here are showing me a lot on how the DateAdd works so I very much appreciate that.
And like the saying goes "Give a man a fish he will eat for a day, Teach him to fish he will eat for life" and that is the same with Gambas I have managed (With help from everyone on here) to do some VERY advanced Functions and features that I could only dream about on the Windows version of my Point of sale system.