Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function PayPeriodStartDate(sDate) 2

Status
Not open for further replies.

JBL312

IS-IT--Management
Jan 14, 2004
56
US
Function PayPeriodStartDate(sDate)
'This function outputs the start of the pay period that sDate falls in.
'Pay Period Start Day: Sunday
'Pay Period End Day: Saturday
'Pay periods consist of a total of 14 days.
'Pay Period Ranges for 2005 thus far:
' 01-02-2005 – 01-15-2005
' 01-16-2005 – 01-29-2005
' 01-30-2005 – 02-12-2005
' 02-13-2005 – 02-26-2005

Dim sFirstEverPayPeriodStartDate
sFirstEverPayPeriodStartDate = "01-02-2005"
End Function

Response.Write(PayPeriodStartDate(“2/15/2005”))
Output should be: 2/13/2005


The above "code" is merely to help you visualize my problem.

As the description of the function tells, I am trying to figure out the date of the first day in a pay period that a given date falls in. I do NOT want to hard code the pay periods. I am having a hard time wrapping my head around the logic needed to determine the date.

I’m just not sure what I need to add to varA, subtract from varB, multiply varC by, in order to get varX. If I can get help with a formula to determine the date, I should be able to code it alright.

If you would be able to help, I would greatly appreciate it. Thank you for your time.
 
hmmmm.... the basic code in the function you want to be able to assign a pay period to whichever date it falls under. i would create a datefunction using the

datediff(Interval, Date1, Date2, FirstDayofWeek, FirstWeekofYear)


Code:
ie.

<%=DateDiff("d", Date, "1/1/2000") %>

YYYY - year
Q - quarter
M - Month
Y - Day of year
D - Day
W - Weekday
WW - Week of year
H - hour
M - Minute
S - Seconds.

caculating the numbr

else trying using datevalue to change your string to a date so you can do arithmetic first and use a case state ment to declare which condition to use depending on month.
hope that helps
 
It looks like your pay period happens every 2 weeks on a Sunday, right?

Off the top of my head, you can try this logic flow, although I don't think it's very efficient:

[ol][li]Figure out what the Sunday (let's call that dtmStartOfWeek for reference) is for the week of the current date
you can do this with the following VBscript:
Code:
dtmStartOfWeek = Date - Weekday(Date, 1)
(check out for reference on the Weekday function)[/li]
[li]Calcualte the number of weeks that dtmStartOfWeek is from a given Pay Period Start Date (Say 01-02-05 according to your example)... this is hard-coded into your logic

do this by:
Code:
intDateDiff = DateDiff("WW","01/02/2005",dtmStartOfWeek)
[/li]
[li]if that number of weeks (intDateDiff) is even, then dtmStartOfWeek is the start date of the pay period. If the number of weeks is odd, then count back 1 week from dtmStartOfWeek[/li][/ol]

I haven't tested this... it's just something off the top of my head...

Work with it and see if you can get what you are looking for.

Earnie Eng
 
I have done something "like" this. In particular, using an interval which doesn;t follow the calendar well presents the issue of always needing to reference some prior (known) date from which to track (calculate). The "? Weeks" is (of course) the quintessiential example of this. All that gibberish being said, once any such date is extablished, the remainig calculation is simply a set of datediffs from that point to the given date.

e.g. "establish" 1/5/2000 as the first Date (1st Sunday; Start of "reference" Pay Period)

While fraught with additional issues / consideration, consider:

Code:
Public Function basStrtPayPer(dtIn As Date) As Date

    Const dtFirstPay = #1/5/2000#
    Dim PaysDiff As Long

    PaysDiff = DateDiff("w", dtFirstPay, dtIn) / 2

    basStrtPayPer = DateAdd("w", PaysDiff * 14, dtFirstPay)

End Function

as a brief starting point / example of the CONCEPT. I am sure that several additional considerations are necessary, but (porffessoial tone here ... ) the proof is left to the student.






MichaelRed


 
Ahh, the old pay period ploy. As MichaelRed said, it is
fraught with additional issues.
My experience with bi-weekly pay periods (US government employer) and trying to create a function was something of a headache because "Administrative Office" in DC made what seemed to be arbitrary decisions as to when an adjustment would be made to compensate for the extra day each year (2 days in Leap Year). I finally had to resort to maintaining the 1st day of the 1st Pay Period of each year in a table because no one would commit to a predictable formula for handling these extra days of the year. That table became my calculation reference point instead of trying to use a fixed point in code.
 
I would like to thank you all for replying to my post. I must say that I was surprised to receive so much input. I greatly appreciate it, thank you.

I will take what you all have said and see if I can complete the ideas and code to produce a working formula. I will post the code as soon as I get it working.

Thank you again for all your thoughts. If you think of anything additional, please fell free to comment.
 
Once again, I would like to thank you all for helping me out with this problem.

The following two functions are used to calculate the beginning date of the pay period that a given date falls in.

The code does not seem to work once the date reaches 2006. If you have any thoughts on the code, good or bad, please let me know.

Code:
<%
    Function IsEven(ByVal iNumber)
        Dim iResult
        iResult = iNumber Mod 2

        If iResult = 1 Then
            IsEven = False
        Else
            IsEven = True
        End If
    End Function

    Function StartOfPayPeriod(ByVal dDateIn)
        'This function outputs the start of the pay period that dDateIn falls in.
        Const dBasePayPeriod = #1/2/2005#
        Dim dDate
        Dim dWeekStart
        Dim iDateDiff

        dDate = cDate(FormatDateTime(dDateIn, 2))
        dWeekStart = (dDate + 1) - Weekday(dDate, 1)
        iDateDiff = Round(DateDiff("WW", dDate, dBasePayPeriod) * -1)

        If IsEven(iDateDiff) Then
            StartOfPayPeriod = dWeekStart
        Else
            StartOfPayPeriod = DateAdd("d", -7, dWeekStart)
        End If
    End Function

    Response.Write(StartOfPayPeriod(Now()))
%>
 
I tested your code and it seems to work okay for 2006 for me - at least it returns 1/1/2006 as the beginning of the the pay period which is a Sunday. If that not correct, please explain.
 
Ok, i couldn't help it, here is a one line solution :)
Code:
Function PayPeriodStart(aDate)
	PayPeriodStart = DateAdd("d",(DateDiff("d",#1/2/2005#,aDate) mod 14) * -1,aDate)
End Function

I think I read your description correctly. basically what this does is first find the number of days since the base date. It then mods that by 14 (to figure out where in the current two-week pay schedule we are) and ten subtracts that from the curent date to bring us back to the start of the current pay period. Should technically work forever i would think and cuts down on all the pesky variable declaration andif-checks ;)

-T

barcode_1.gif
 
There have been a number of problems as of late, which have been solved by using the Mod function. I think that this is all pointing to the fact that I need to work more with the function and actually learn it and not just think I know how it works.

My understanding of Mod is that it takes number A and divides it by number B. The resulting remainder, if any, is output. I believe that there is also some rounding taking place. I am however completely confused as to how 40 Mod 14 is equal to 12.

40 / 14 is 2.8571428571428571428571428571429
How is Mod getting 12 out of this?

Anyway, I appreciate the one line function. I do not fully follow it due to the Mod function but after testing, it does work correctly.

You all have really helped me out, thank you.
 
It seems you have the definition of Mod down, but not necessarally an understanding of it (don't worry, I think everyone runs into this at some point).

basically your correct, Mod divides the first number by the second and returns the whole number remainder. If you remember way back in the early dawn of time when you were first learning division, that may help a bit. I'll do your example and explain it:
40 mod 14: divide 40 by 14 and return the whole number remainder
[tt]
1) Divide the first step, 14 into 40 which is 2
___2__
14 | 40

2) calculate the remainder
___2__
14 | 40
-28
-----
12

3) if we were doing decimal math we would then continue to tack on a decimal and 0 on the 40 and 12 and a decimal on the 2
___2.__
14 | 40.0
-28
-----
120

4) etc, continue to whatever level of accuracy you want
___2.85__
14 | 40.00
-28
-----
120
-112
--------
80
-70
--------
10
...[/tt]

basically with decimal-based math the division continues indefinately until either a solution is reached or you decide to stop. With a mod you stop before you add a decimal point and decimal places to the original number. So in your 40 mod 14 question you end up with a value of 2 remainder 12 (2 * 14 + 12 = 40).

With a more complex one (like 155) you would get 11r1 (11 remainder 1).

I'm not sure if I explained that well enough...let me know if not and I can try a differant approach.

Another good funciton to know that helps complement mod is Fix. Fix actually has nothing to do with mod but can be used to find the first part of the result as well as formany other things. Fix(x) basically drops any decimal value from x and returns the whole number. This may sound like round at first, but Fix doesn't do any rounding, just straight drop of the decimal.
So Fix(1.99) = 1
Fix(1.01) = 1
etc

The reason i bring this up is because you can use it as a complement, like so:
decimal_value = x / y
remainder = x mod y
int_division_value = Fix(x/y)

so decimal value would eb teh decimal value of a standard division. remainder we went over. int_division_value would be the part before the remainder in our above example.

Example:
Code:
Response.Write "40/14 = " & Fix(40/14) & " remainder " & Mod(40/14)
basically an easy way to get the actual divided amount when you want to do integer-based math or at least make it look like your doing integer based math.


here is a common example that uses both methods, converting seconds into hours, minutes, seconds:
Code:
Function TimeString(seconds)
   Dim hours, minutes
   'num of hours = number of ties we can evenly divide seconds by 3600
   hours = Fix(seconds/3600)

   'we need to update the seconds with the remainder that wasn't used for whole hours
   seconds = seconds mod 3600

   'minutes similar to hours, but number of times we can evenl;y divide seconds by 60
   minutes = Fix(seconds/60)

   'final adjustment - we need the left over seconds
   seconds = seconds mod 60

   'just a return value
   TimeString = hours & ":" & minutes & ":" & seconds
End Function
normally I would also pad those values, but I didn't want to complicate the example more then necessary.

Hope this helped somewhat in your understanding of mod rather then everyone elses understanding of my mind working slightly off kilter or some such :)

-T

barcode_1.gif
 
I am extremely impressed with the level of explanation you gave in order to answer my question. Your post must have taken a good bit of time and thought to put together. Thank you for that. I now have a much better understanding of the Mod function and do not see myself having much more of a problem when I need to use it again.

Thank you for your explanation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top