[Solved] Get the Previous Sunday from a Date

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1128
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: [Solved - sort of] Get the Previous Sunday from a Date

Post by cogier »

That works for me.
User avatar
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

Post by grayghost4 »

Like a dog with a bone ... I can't let go until I have finished it :lol:

   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))
User avatar
cogier
Site Admin
Posts: 1128
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: [Solved - sort of] Get the Previous Sunday from a Date

Post by cogier »

grayghost4 wrote: Tuesday 2nd April 2024 2:42pm Like a dog with a bone ... I can't let go until I have finished it :lol:

   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.

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
User avatar
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

Post by grayghost4 »

I read the original post as wanting the sunday of the last full week to print the last week activity report.
as 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)
Mabe I did not understand what he needed ;)

for me it was a good exersize and learing how to use some of the date functions :D

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))
User avatar
BruceSteers
Posts: 1588
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: [Solved - sort of] Get the Previous Sunday from a Date

Post by BruceSteers »

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.
as 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)
Mabe I did not understand what he needed ;)

for me it was a good exersize and learing how to use some of the date functions :D

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 think he wants a few things, as soon as i solved one issue he asked for another variant.

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
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: [Solved - sort of] Get the Previous Sunday from a Date

Post by AndyGable »

BruceSteers wrote: Wednesday 3rd April 2024 3:42pm
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.
as 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)
Mabe I did not understand what he needed ;)

for me it was a good exersize and learing how to use some of the date functions :D

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 think he wants a few things, as soon as i solved one issue he asked for another variant.

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.
Post Reply