Need Calendar Help

Just starting out? Need help? Post your questions and find answers here.
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Need Calendar Help

Post by Alex »

Has anyone written a routine to find the day before the current date? In other words, if it's April 3rd, 2004, I need a routine that give me April 2nd, 2004. This needs to handle such issues as leap years, months with 31, 30, or 28 days, etc.

Thanks,

Alex
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Need Calendar Help

Post by PB »

Code: Select all

today=Date()
yesterday=AddDate(today,#PB_Date_Day,-1)
Debug FormatDate("%dd-%mm-%yyyy",yesterday)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Re: Need Calendar Help

Post by Alex »

Cool! Thankyou!

Alex
PB wrote:

Code: Select all

today=Date()
yesterday=AddDate(today,#PB_Date_Day,-1)
Debug FormatDate("%dd-%mm-%yyyy",yesterday)
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

Good reply and example, PB.

Now here is a bit more of a challenge for you.

I need to determined the date of the second Friday following any given date. Example: Today is 10 Apr. I need the date of the second folloing Friday, 21 Apr.

How simple can you make that one?

Terry
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@TerryHough: Need some more info first -- if today was Friday, does that
mean you want to get next Friday's date, or two Friday's later?

Edit: Anyway, here's what I have for now:

Code: Select all

today=Date() : friday=today
; Next line gets this Friday's date.
Repeat : friday=AddDate(friday,#PB_Date_Day,1) : check=DayOfWeek(friday) : Until check=5
friday=AddDate(friday,#PB_Date_Day,7) ; Now get the next Friday after the one just found.
Debug "Today is "+FormatDate("%dd-%mm-%yyyy",today)
Debug "Two Fridays away is "+FormatDate("%dd-%mm-%yyyy",friday)
Last edited by PB on Mon Apr 10, 2006 10:17 pm, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Need Calendar Help

Post by PB »

@Fred: This example...

Code: Select all

yesterday=AddDate(today,#PB_Date_Day,-1)
...leads me to request that AddDate be renamed to something like CalcDate
instead, as we're not always adding to the date at all. What do you think?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

PB wrote:@TerryHough: Need some more info first -- if today was Friday, does that
mean you want to get next Friday's date, or two Friday's later?
If today is Friday, I still want the second following Friday.

Thanks for your example.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Try this one Terry...

Code: Select all

#Friday = 5

Procedure GetSecondFriday(xDate)
  dow = DayOfWeek(xDate)
  If dow < #Friday
    secondFriday = AddDate(xDate, #PB_Date_Day, 7 + (#Friday - dow))
  Else
    secondFriday = AddDate(xDate, #PB_Date_Day, 14 - (dow - #Friday))
  EndIf
  ProcedureReturn secondFriday
EndProcedure

checkDate = Date(2006, 4, 10, 0, 0, 0)
checkDate$ = FormatDate("%mm/%dd/%yyyy", checkDate)

MessageRequester("Second Friday from " + checkDate$ + " will be", FormatDate("%mm/%dd/%yyyy", GetSecondFriday(checkDate))) 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

Thanks Sparkie!

Here is what I am using, includes features of yours and PB's and a bit
more efficient than either.

Code: Select all

SecondFriday = AddDate(Date(),#PB_Date_Day,9)
dow = DayOfWeek(SecondFriday)
If dow - 5                                                     ; Computed date is not a Friday
  SecondFriday = AddDate(SecondFriday, #PB_Date_Day,(5 - dow)) ; Adjust to following Friday
EndIf   
MessageRequester("Debug","The second Friday after today is " +FormatDate("%mm-%dd-%yyyy",SecondFriday),0) 
Thanks guys.
Terry
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@TerryHough: Mine was just a quick rush job before I left for work (that's
why I said I need more info first). But when I got home, Sparkie had posted
a better one, so I just didn't bother fixing mine. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@PB,
You solution was a big help! As was Sparkie's code.

I actually had a need for this, believe it or not. :D And this solution is
much simpler and elegant than the way I was previously doing it.

Thanks again,
Terry
Post Reply