Page 1 of 1

Need Calendar Help

Posted: Mon Apr 10, 2006 3:24 am
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

Re: Need Calendar Help

Posted: Mon Apr 10, 2006 3:58 am
by PB

Code: Select all

today=Date()
yesterday=AddDate(today,#PB_Date_Day,-1)
Debug FormatDate("%dd-%mm-%yyyy",yesterday)

Re: Need Calendar Help

Posted: Mon Apr 10, 2006 4:55 pm
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)

Posted: Mon Apr 10, 2006 5:39 pm
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

Posted: Mon Apr 10, 2006 10:09 pm
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)

Re: Need Calendar Help

Posted: Mon Apr 10, 2006 10:14 pm
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?

Posted: Mon Apr 10, 2006 11:03 pm
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.

Posted: Tue Apr 11, 2006 12:02 am
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))) 

Posted: Tue Apr 11, 2006 6:12 pm
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

Posted: Tue Apr 11, 2006 10:08 pm
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. :)

Posted: Tue Apr 11, 2006 10:17 pm
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