[Solved] Get the Previous Sunday from a Date

Post your Gambas programming questions here.
Online
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Get the Previous Sunday from a Date

Post by AndyGable »

Ive gone another route now I am using 2 date pickers so the user can select what ever dates they want

I will come back to this once my head is not pounding any more
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post by BruceSteers »

Sigh

Not sure what you could not get your head around or why after asking 3 different questions that all got answered you did something completely different? i guess that's your prerogative.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post by BruceSteers »

Okay so i had a real proper read of your first post to see what the initial goal was again.

You want to get from Sunday to Monday of a previous week from a given date.

So get the previous Sunday , then the Monday before that...

So you want something like this...

Public Sub Button1_Click()

Dim d As Date = Now

Dim dLastSun As Date = GetPreviousSunday(d)
Dim dLastMon As Date = DateAdd(dLastSun, gb.Day, -6) ' just subtract 6 days to get to the monday

  Print "From:";; Format(dLastSun, "dddd dd/mm/yyyy");; "To:";;
  Print Format(dLastMon, "dddd dd/mm/yyyy")

End


Public Sub GetPreviousSunday(hDate As Date) As Date
 
  Dim d As Date
  Dim iDay As Integer = WeekDay(hDate)
  If Not iDay Then iDay = 7  ' if given date is a sunday go back to previous week
  d = DateAdd(hDate, gb.Day, -iDay) ' Subtract days to go to the Sunday before the given date
 
  Return d
 
End



From: Sunday 24/03/2024 To: Monday 18/03/2024
If at first you don't succeed , try doing something differently.
BruceS
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

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

Post by thatbruce »

No more clues from me. If you want us to writ your damn pos system, send money. :x
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1579
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 »

thatbruce wrote: Wednesday 27th March 2024 11:22am No more clues from me. If you want us to writ your damn pos system, send money. :x
Haha :D

Yep, give a man a fish and he eats for a day. And all that 😉
If at first you don't succeed , try doing something differently.
BruceS
Online
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 »

thatbruce wrote: Wednesday 27th March 2024 11:22am No more clues from me. If you want us to writ your damn pos system, send money. :x
I am sorry If I upset anyone I was in a lot of pain last night with my head and I was not understanding anything I was planning on having the date range selection anyway so all i did was implemented that as it was a quicker thing now my head is not hurting i can have a look as to what I was doing wrong as to why I was never getting the correct dates.

I mean yesterday I was getting Wednesday 13th one time and then I was getting the 3/3 the next time the report was run so I am 99.99% convinced I had coded it wrong or i changed something that should not have been changed

So Tomorrow when it is Daylight and ive had more sleep I shall be returning to the code (as I like to have a button that just says "Tender Report Last week" and the customer can just press it to get the previous weeks report.

Public Sub Button1_Click()
 
Dim d As Date = Now
 
Dim dLastSun As Date = GetPreviousSunday(d)
Dim dLastMon As Date = DateAdd(dLastSun, gb.Day, -6) ' just subtract 6 days to get to the monday
 
  Print "From:";; Format(dLastSun, "dddd dd/mm/yyyy");; "To:";;
  Print Format(dLastMon, "dddd dd/mm/yyyy")
 
End


@BruceSteers is my line of thought right if I wanted to get the current Monday to Sunday Dates (this weeks) I would change the -6 to a 6 for this Sundays date? or Do I need to change something else with in the GetPreviousSunday Function?
User avatar
BruceSteers
Posts: 1579
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 »

Try it and see
If at first you don't succeed , try doing something differently.
BruceS
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 may be wrong .. but this seems to get the previous sunday.
But I am probably not understanding what you are trying to do ;)


Print DateAdd(Now, gb.Day, -(WeekDay(Now) + 7))
  
User avatar
cogier
Site Admin
Posts: 1127
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: Saturday 30th March 2024 4:44pm I may be wrong .. but this seems to get the previous sunday.

Print DateAdd(Now, gb.Day, -(WeekDay(Now) + 7))
  
This doesn't quite get it right. Using the following code I get the Sunday before last Sunday!

Print Now
  Print DateAdd(Now, gb.Day, -(WeekDay(Now) + 7))
  


Result:-
30/03/2024 16:51:29
17/03/2024 16:51:29
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 »

how about this :

Public Sub Button1_Click()
Dim d As Date = Date(Now)
Dim iDay As Integer = WeekDay(d)
If iDay = 6 Then iDay = -1    ' ' move Saturday 
Print d
Print DateAdd(d, gb.Day, -(iDay)  -7)
End 


results:

03/30/2024 00:00:00
03/24/2024 00:00:00
Last edited by grayghost4 on Sunday 31st March 2024 12:07pm, edited 1 time in total.
Post Reply