# Thread: function to manipulate date

1. ## function to manipulate date

Hi all,
I need help with a function which takes the type of interval, positive or negative interval, and a date as arguments. You can add or subtract days, weeks, months, quarters, or years.
Basically I need a function to subtract or add days, weeks, months, years to a given date. This function should handle 28/29/30/31 days, leap year scenarios also..
Any help with existing function or a new function will be greatly appreciated..

Thanks
Ray

2. ## Re: function to manipulate date

Maybe this will help. Works with days, months, years.

[+] datetime AddDatePeriod(datetime dDate, string sType, string sInterval)
[ ] list of int lsDuration = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
[ ]
[ ] datetime dResult = dDate
[ ] int iInt = Val(sInterval)
[ ] int iY = GetDateTimePart(dResult, DTP_YEAR)
[ ] int iM = GetDateTimePart(dResult, DTP_MONTH)
[ ] int iD = GetDateTimePart(dResult, DTP_DAY)
[ ]
[+] switch Lower(sType)
[-] case "year"
[ ] iY = iY + iInt
[-] case "month"
[ ] iM = iM + iInt
[-] case "day"
[ ] iD = iD + iInt
[ ]
[+] while iM &gt; 12
[ ] iY = iY + 1
[ ] iM = iM - 12
[ ]
[+] while iM &lt;= 0
[ ] iY = iY - 1
[ ] iM = iM + 12
[ ]
[ ] int iT = iY / 4
[ ] iT = iT * 4
[ ]
[-] if iY == iT
[ ] lsDuration[2] = 29
[ ]
[+] while iD &lt;= 0
[ ] iD = iD + lsDuration[iM]
[ ] iM = iM - 1
[ ]
[+] while iM &lt;= 0
[ ] iY = iY - 1
[ ] iM = iM + 12
[ ]
[+] while iD &gt; lsDuration[iM]
[ ] iD = iD - lsDuration[iM]
[ ] iM = iM + 1
[ ]
[+] while iM &gt; 12
[ ] iY = iY + 1
[ ] iM = iM - 12
[ ]
[ ]
[ ] dResult = MakeDateTime(iY, iM, iD)
[ ]
[ ] return dResult

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.