Page 2 of 3
Re: Get the Previous Sunday from a Date
Posted: Wednesday 27th March 2024 4:37am
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
Re: [Solved] Get the Previous Sunday from a Date
Posted: Wednesday 27th March 2024 8:45am
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.
Re: [Solved] Get the Previous Sunday from a Date
Posted: Wednesday 27th March 2024 9:36am
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
Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Wednesday 27th March 2024 11:22am
by thatbruce
No more clues from me. If you want us to writ your damn pos system, send money.
Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Wednesday 27th March 2024 9:17pm
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.
Haha
Yep, give a man a fish and he eats for a day. And all that
Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Thursday 28th March 2024 12:48am
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.
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?
Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Thursday 28th March 2024 5:48pm
by BruceSteers
Try it and see
Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Saturday 30th March 2024 4:44pm
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))
Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Saturday 30th March 2024 4:55pm
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
Re: [Solved - sort of] Get the Previous Sunday from a Date
Posted: Saturday 30th March 2024 11:13pm
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