[Solved] Get the Previous Sunday from a Date

Post

Posted
Rating:
#1 (In Topic #1214)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi All

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)

  1. Private Function GetPreviousWeekDayDate(inputDate As Date) As Date
  2.     ' Calculate the number of days to subtract to find the previous week day
  3.     Dim daysToSubtract As Integer
  4.     daysToSubtract = WeekDay(inputDate + 5) Mod 7
  5.  
  6.     ' Subtract the calculated days from the input date
  7.     Dim previousWeekDayDate As Date
  8.     previousWeekDayDate = inputDate - daysToSubtract
  9.  
  10.     Return previousWeekDayDate

Code (gambas)

  1. Private Function GetSundayDate(inputDate As Date) As Date
  2.     ' Calculate the number of days to subtract to find the Sunday date
  3.     Dim daysToSubtract As Integer
  4.     daysToSubtract = WeekDay(inputDate) - 1 ' Sunday is 1, Saturday is 7
  5.  
  6.     ' Subtract the calculated days from the input date
  7.     Dim sundayDate As Date
  8.     sundayDate = inputDate - daysToSubtract
  9.  
  10.     Return sundayDate
  11.  

This is how i am calling them at the moment

 

Code (gambas)

  1.        dtStartDate = GetPreviousWeekDayDate(Now)
  2.           dtEndDate = GetSundayDate(dtStartDate)
  3.  
  4.     Dim StartOfWeekDay As String = "Monday " & Format(dtStartDate, "dd/mm/yyyy")
  5.       Dim EndOfWeekDay As String = "Sunday " & Format(dtEndDate, "dd/mm/yyyy")

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
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
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

EDIT: sorry i had the gb.Week/gb.Day and the values round the wrong way.

Code (gambas)

  1.  
  2. Private Function GetPreviousWeekDayDate(inputDate As Date) As Date
  3.  
  4.     Return DateAdd(inputDate, gb.Week, -1)
  5.  
  6.  
  7.  

And DateAdd again minus the days

Code (gambas)

  1.  
  2. Private Function GetSundayDate(inputDate As Date) As Date
  3.     ' Calculate the number of days to subtract to find the Sunday date
  4.     Dim daysToSubtract As Integer
  5.     daysToSubtract = WeekDay(inputDate) - 1 ' Sunday is 1, Saturday is 7
  6.  
  7.     ' Subtract the calculated days from the input date
  8.     Dim sundayDate As Date
  9.     sundayDate = DateAdd(inputDate, gb.Day, -daysToSubtract)
  10.  
  11.     Return sundayDate
  12.  
  13.  
  14.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘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

Code (gambas)

  1.  
  2. Private Function GetPreviousWeekDayDate(inputDate As Date) As Date
  3.  
  4.     Return DateAdd(inputDate, -1, gb.Week)
  5.  
  6.  
  7.  

And DateAdd again minus the days

Code (gambas)

  1.  
  2. Private Function GetSundayDate(inputDate As Date) As Date
  3.     ' Calculate the number of days to subtract to find the Sunday date
  4.     Dim daysToSubtract As Integer
  5.     daysToSubtract = WeekDay(inputDate) - 1 ' Sunday is 1, Saturday is 7
  6.  
  7.     ' Subtract the calculated days from the input date
  8.     Dim sundayDate As Date
  9.     sundayDate = DateAdd(inputDate, -daysToSubtract, gb.Day)
  10.  
  11.     Return sundayDate
  12.  
  13.  
  14.  

Thank-you BruceSteers

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?
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
The last day of the month is the 1st day of the next month minus one day.

Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘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)
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
So i spent a whole minute writing this.  (thanks to the clues given by the other Bruce :) )

Code (gambas)

  1.  
  2. Public Sub Button1_Click()
  3.  
  4.   Debug GetLastMonthDay(Now)
  5.   Debug Format(GetLastMonthDay(Now), "dddd dd mmm")
  6.  
  7.  
  8.  
  9.  
  10. Public Sub GetLastMonthDay(hDate As Date) As Date
  11.  
  12.   Dim d As Date
  13.  
  14.   d = DateAdd(hDate, gb.Day, 1 - Day(hDate))  ' set day to first of the month
  15.   d = DateAdd(d, gb.Month, 1)  ' advance 1 month (will be first of next month leap year or not)
  16.   d = DateAdd(d, gb.Day, -1)  ' subtract one day
  17.  
  18.   Return d
  19.  
  20.  
  21.  

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)
Online now: No Back to the top

Post

Posted
Rating:
#7
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

So i spent a whole minute writing this.  (thanks to the clues given by the other Bruce :) )

Code (gambas)

  1.  
  2. Public Sub Button1_Click()
  3.  
  4.   Debug GetLastMonthDay(Now)
  5.   Debug Format(GetLastMonthDay(Now), "dddd dd mmm")
  6.  
  7.  
  8.  
  9.  
  10. Public Sub GetLastMonthDay(hDate As Date) As Date
  11.  
  12.   Dim d As Date
  13.  
  14.   d = DateAdd(hDate, gb.Day, 1 - Day(hDate))  ' set day to first of the month
  15.   d = DateAdd(d, gb.Month, 1)  ' advance 1 month (will be first of next month leap year or not)
  16.   d = DateAdd(d, gb.Day, -1)  ' subtract one day
  17.  
  18.   Return d
  19.  
  20.  
  21.  

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 BruceSteers

I think I may have broke my Sub that I have been using to get the first Day of the month

Code (gambas)

  1. Private Function GetfirstDayOfMonth(MonthName As Integer) As String
  2.     Dim D As String
  3.  
  4.     If MonthName = 12 Then Year -= 1
  5.    
  6.     D = "01/" & Format(MonthName, "00") & "/" & Year
  7.  
  8.     Return D
  9.  End

It returns for Feb 02/01/2024 (i know that is correct but I need to have it formatted as dd/mm/yyyy)
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
That cannot be true

you make D a string object not a Date object.
Then you make the string and return it…

Code (gambas)

  1. D = "01/" & Format(MonthName, "00") & "/" & Year
  2.  
  3.  

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…

Code (gambas)

  1. Dim DD As Date = Now
  2.  
  3. Print "DD="; DD
  4. Print "DS="; DS
  5.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Enthusiast
AndyGable is in the usergroup ‘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…

Code (gambas)

  1. D = "01/" & Format(MonthName, "00") & "/" & Year
  2.  
  3.  

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"??
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
 I'm not sure what you mean by Hover over them?
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")
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#13
Guru
BruceSteers is in the usergroup ‘Guru’
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…

Code (gambas)

  1.  
  2. Public Sub Button1_Click()
  3.  
  4.  
  5. Dim dLastSun As Date = GetPreviousSunday(d)
  6. Dim dLastMon As Date = DateAdd(dLastSun, gb.Day, -6) ' just subtract 6 days to get to the monday
  7.  
  8.   Print "From:";; Format(dLastSun, "dddd dd/mm/yyyy");; "To:";;
  9.   Print Format(dLastMon, "dddd dd/mm/yyyy")
  10.  
  11.  

Code (gambas)

  1.  
  2. Public Sub GetPreviousSunday(hDate As Date) As Date
  3.  
  4.   Dim d As Date
  5.   Dim iDay As Integer = WeekDay(hDate)
  6.   If Not iDay Then iDay = 7  ' if given date is a sunday go back to previous week
  7.   d = DateAdd(hDate, gb.Day, -iDay) ' Subtract days to go to the Sunday before the given date
  8.  
  9.   Return d
  10.  
  11.  
  12.  

From: Sunday 24/03/2024 To: Monday 18/03/2024
Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
No more clues from me. If you want us to writ your damn pos system, send money. :x

Online now: No Back to the top

Post

Posted
Rating:
#15
Guru
BruceSteers is in the usergroup ‘Guru’

thatbruce said

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 <EMOJI seq="1f609" tseq="1f609">😉</EMOJI>
Online now: No Back to the top

Post

Posted
Rating:
#16
Enthusiast
AndyGable is in the usergroup ‘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)

  1. Public Sub Button1_Click()
  2.  
  3.  
  4. Dim dLastSun As Date = GetPreviousSunday(d)
  5. Dim dLastMon As Date = DateAdd(dLastSun, gb.Day, -6) ' just subtract 6 days to get to the monday
  6.  
  7.   Print "From:";; Format(dLastSun, "dddd dd/mm/yyyy");; "To:";;
  8.   Print Format(dLastMon, "dddd dd/mm/yyyy")
  9.  
  10.  

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?
Online now: No Back to the top

Post

Posted
Rating:
#17
Guru
BruceSteers is in the usergroup ‘Guru’
Try it and see
Online now: No Back to the top

Post

Posted
Rating:
#18
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
I may be wrong .. but this seems to get the previous sunday.
But I am probably not understanding what you are trying to do  ;)


Code (gambas)

  1. Print DateAdd(Now, gb.Day, -(WeekDay(Now) + 7))
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#19
Avatar
Guru
cogier is in the usergroup ‘Guru’

grayghost4 said

I may be wrong .. but this seems to get the previous sunday.

Code (gambas)

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

This doesn't quite get it right. Using the following code I get the Sunday before last Sunday!

Code (gambas)

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

Result:-
30/03/2024 16:51:29
17/03/2024 16:51:29
Online now: No Back to the top

Post

Posted
Rating:
#20
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
how about this :

Code (gambas)

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

results:

03/30/2024 00:00:00
03/24/2024 00:00:00
Online now: No Back to the top

Post

Posted
Rating:
#21
Avatar
Guru
cogier is in the usergroup ‘Guru’
That works for me.
Online now: No Back to the top

Post

Posted
Rating:
#22
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
Like a dog with a bone … I can't let go until I have finished it  :lol:

Code (gambas)

  1.    Dim iDay As Integer = WeekDay(Now)
  2.    If iDay = 6 Then iDay = -1    ' Move sat to begining of week
  3.    Print Date(DateAdd(Now, gb.Day, -(iDay + 1) - gb.Week))
  4.  
Online now: No Back to the top

Post

Posted
Rating:
#23
Avatar
Guru
cogier is in the usergroup ‘Guru’

grayghost4 said

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

Code (gambas)

  1.    Dim iDay As Integer = WeekDay(Now)
  2.    If iDay = 6 Then iDay = -1    ' Move sat to begining of week
  3.    Print Date(DateAdd(Now, gb.Day, -(iDay + 1) - gb.Week))
  4.  

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)

  1. Public Sub Form_Open()
  2.  
  3.   Print GetLastSunday(Now)
  4.   Print GetLastSunday(Date(2024, 4, 1))
  5.   Print GetLastSunday(Date(2024, 3, 31))
  6.   Print GetLastSunday(Date(2024, 3, 25))
  7.   Print GetLastSunday(Date(2024, 3, 23))
  8.  
  9.  
  10. Public Sub GetLastSunday(dDate As Date) As String
  11.  
  12.   Dim sDay As Integer = WeekDay(dDate)
  13.  
  14.   If sDay = 0 Then sDay = 7
  15.   Return Format(DateAdd(dDate, gb.Day, -sDay), "dddd d mmmm yyyy")
  16.  
Online now: No Back to the top

Post

Posted
Rating:
#24
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
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

Code (gambas)

  1.    If iDay = 6 Then iDay = -1    ' Move sat to begining of week
  2.    Print Date(DateAdd(Now, gb.Day, -(iDay) - gb.Week))
  3.    Print Date(DateAdd(Now, gb.Day, -iDay))
  4.  
Online now: No Back to the top

Post

Posted
Rating:
#25
Guru
BruceSteers is in the usergroup ‘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  :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

Code (gambas)

  1.    If iDay = 6 Then iDay = -1    ' Move sat to begining of week
  2.    Print Date(DateAdd(Now, gb.Day, -(iDay) - gb.Week))
  3.    Print Date(DateAdd(Now, gb.Day, -iDay))
  4.  

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 :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.