[Solved] Get the Previous Sunday from a Date
Posted
#1
(In Topic #1214)
Enthusiast

I need some help
I am trying to get the Previous sunday from a date (for example 23/03/2024 should return me the sunday date of 17th March)
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)
This is what I have so for
Code (gambas)
- ' Calculate the number of days to subtract to find the previous week day
- ' Subtract the calculated days from the input date
- previousWeekDayDate = inputDate - daysToSubtract
- Return previousWeekDayDate
Code (gambas)
- ' Calculate the number of days to subtract to find the Sunday date
- ' Subtract the calculated days from the input date
- sundayDate = inputDate - daysToSubtract
- Return sundayDate
This is how i am calling them at the moment
But I am getting at the moment getting Start Date : Monday 19/03/2024 and End Date : Sunday 18/03/2024
if anyone has any better ideas as how to get previous weeks dates I would be most grateful as I have been on this report Since Saturday afternoon
Posted
Guru

You must use DateAdd()
/lang/dateadd - Gambas Documentation
DateAdd() minus 1 week
EDIT: sorry i had the gb.Week/gb.Day and the values round the wrong way.
And DateAdd again minus the days
Code (gambas)
- ' Calculate the number of days to subtract to find the Sunday date
- ' Subtract the calculated days from the input date
- Return sundayDate
Posted
Enthusiast

BruceSteers said
You cannot use a Date object like that by just adding/subtracting integers from it.
You must use DateAdd()
/lang/dateadd - Gambas Documentation
DateAdd() minus 1 week
And DateAdd again minus the daysCode (gambas)
' Calculate the number of days to subtract to find the Sunday date ' Subtract the calculated days from the input date Return sundayDate
Thank-you
How would I change the Sunday Function to get the last Day of the Month for a given month? example say I want to get the last day of March (31st) is this possible with Gambas?
Posted
Regular

Posted
Enthusiast

thatbruce said
The last day of the month is the 1st day of the next month minus one day.
yea I know but does that take into account leaps years?
in VB.net I can do something like Date = Month.firstday and it will retun
01/01 or 01/02 etc and then I could do Month.lastday and it would return for March 31.03
I need to beable to get this working as they are used for Reports in my system (I am not sure yet how I will hand week that bridge two months) example of this would be 31st Jan (Wednesday) and the 1st Feb (Thursday)
I am at the moment assuming the sql between statement would work (I have no data yet for the time frame to test it on)
Posted
Guru

Code (gambas)
- Return d
Form1.Button1_Click.8: 31/03/2024 01:22:35
Form1.Button1_Click.9: Sunday 31 Mar
PS. I edited the original example as i got the gb.Month/gb.Day and the values the wrong way round. i wrote d = DateAdd(d, -1, gb.Month) where i should have written d = DateAdd(d, gb.Month, -1)
Posted
Enthusiast

BruceSteers said
So i spent a whole minute writing this. (thanks to the clues given by the other Bruce)
Code (gambas)
Return d
Form1.Button1_Click.8: 31/03/2024 01:22:35
Form1.Button1_Click.9: Sunday 31 Mar
PS. I edited the original example as i got the gb.Month/gb.Day and the values the wrong way round. i wrote d = DateAdd(d, -1, gb.Month) where i should have written d = DateAdd(d, gb.Month, -1)
That works great Thanks
I think I may have broke my Sub that I have been using to get the first Day of the month
It returns for Feb 02/01/2024 (i know that is correct but I need to have it formatted as dd/mm/yyyy)
Posted
Guru

you make D a string object not a Date object.
Then you make the string and return it…
D will begin with 01/ . just like you made it
Unless somewhere else along the line you are using a Date or Format function on the String object.
You should not mix Date and String , they are not the same even though they look it they are treated differently by the interpreter.
Look at this…
Prints this…
DD=27/03/2024 02:24:00
DS=03/27/2024 02:24:00.203
The date as a Date object is localized the way you want it, the string is not.
You also make Year a String object then use Integer operations on it!
I think I've given all the advice I can on this. All the answers are in the previous code.
Posted
Enthusiast

BruceSteers said
That cannot be true
you make D a string object not a Date object.
Then you make the string and return it…
D will begin with 01/ . just like you made it
Unless somewhere else along the line you are using a Date or Format function on the String object.
You should not mix Date and String , they are not the same even though they look it they are treated differently by the interpreter.
You also make Year a String object then use Integer operations on it!
I think I've given all the advice I can on this. All the answers are in the previous code.
I have HARD coded dtStartDate to be "01/02/2024" but when you hover over dtStartDate it show as "02/01/2024" at least we know why dtEndDate is showing as January 31st lol
Both dtStartDate and dtEndDate are declared as Date So am I doing something else wrong or could be local settings of my PC be overriding the hard coding and making it show up as a "02/01/2024"??
Posted
Guru

Gambas will just display the date object as it is.
If they are using Date objects not Strings then just use Format where you need it formatted
sDisplayString = Format(dtStartDate,"dddd dd/mm/yyyy")
Posted
Enthusiast

I will come back to this once my head is not pounding any more
Posted
Guru

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.
Posted
Guru

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…
Code (gambas)
Code (gambas)
From: Sunday 24/03/2024 To: Monday 18/03/2024
Posted
Regular

Posted
Guru

thatbruce said
No more clues from me. If you want us to writ your damn pos system, send money. :x
Haha
Yep, give a man a fish and he eats for a day. And all that <EMOJI seq="1f609" tseq="1f609">😉</EMOJI>
Posted
Enthusiast

thatbruce said
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.
Code (gambas)
Posted
Guru

Posted
Enthusiast

Posted
Guru

grayghost4 said
This doesn't quite get it right. Using the following code I get the Sunday before last Sunday!
Result:-
30/03/2024 16:51:29
17/03/2024 16:51:29
Posted
Enthusiast

Posted
Guru

Posted
Enthusiast

Posted
Guru

grayghost4 said
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.
Code (gambas)
Posted
Enthusiast

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
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
Posted
Guru

grayghost4 said
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
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
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
1 guest and 0 members have just viewed this.


