Help with Date logic
Moderators: chulett, rschirm, roy
Help with Date logic
Hello,
We have two date fields, D1and D2, representing start and end dates. According to business rules, we need to increase the year of the dates by one but keep the same day and month. If the year increment results in an invalid date, we make the date field null or store any default value.
For example, let us assume D1 is 02/29/12. Now incrementing the year by one makes D1 as 02/29/13 which is invalid and hence we need to make the date to be null (or default value). What is the best method of handling this logic?
Thanks.
We have two date fields, D1and D2, representing start and end dates. According to business rules, we need to increase the year of the dates by one but keep the same day and month. If the year increment results in an invalid date, we make the date field null or store any default value.
For example, let us assume D1 is 02/29/12. Now incrementing the year by one makes D1 as 02/29/13 which is invalid and hence we need to make the date to be null (or default value). What is the best method of handling this logic?
Thanks.
There are many date functions to choose from in a parallel job...
See the Parallel Job Developer's Guide Chapter 2. Designing parallel jobs. Look under the "Date and time formats" section for all the possible date and time format tags available. See Appendix B. Parallel Transform functions for date and time functions to choose from.
See the Parallel Job Developer's Guide Chapter 2. Designing parallel jobs. Look under the "Date and time formats" section for all the possible date and time format tags available. See Appendix B. Parallel Transform functions for date and time functions to choose from.
Choose a job you love, and you will never have to work a day in your life. - Confucius
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Thanks for the replies.
I used the DateFromDaysSince() function and made the offset as 366. This works perfect for dates less than 02/29/12 but for dates greater than and equal to 02/29/12 the offset should be 365. One option would be checking each date to be less or greater than 02/29/12 but I believe there might be better options than this. Any help is greatlly appreciated.
Also, how can the logic be made more generic and not just for year 2012?
I used the DateFromDaysSince() function and made the offset as 366. This works perfect for dates less than 02/29/12 but for dates greater than and equal to 02/29/12 the offset should be 365. One option would be checking each date to be less or greater than 02/29/12 but I believe there might be better options than this. Any help is greatlly appreciated.
Also, how can the logic be made more generic and not just for year 2012?
Another option is the DateOffsetByComponents() function. It takes 4 arguments: your date column, year offset, month offset, day offset. Offsets can be positive or negative. Offset a valid Feb 29 date by plus or minus one year will produce a valid result with date of March 1. It's as if your input date were actually March 1. No special leap year handling required, unless you must detect Feb 29 as input dates, then you can add an If Then Else to handle it.
Code: Select all
DateOffsetByComponents(lnk.D1, 1, 0, 0)
Choose a job you love, and you will never have to work a day in your life. - Confucius
-
- Participant
- Posts: 597
- Joined: Fri Apr 29, 2005 6:19 am
- Location: Singapore
-
- Participant
- Posts: 597
- Joined: Fri Apr 29, 2005 6:19 am
- Location: Singapore
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 597
- Joined: Fri Apr 29, 2005 6:19 am
- Location: Singapore
ah.. Should have phrased it differently.
Kryt0n, 2000 is evenly divisible by 100 and 400, & so it is a leap year.
My other condition is "Not evenly divisible by 100 but evenly divisible by 4". This is applicable for years that are not centenaries..
If none of the above conditions is satisfied, then it is a common year
Kryt0n, 2000 is evenly divisible by 100 and 400, & so it is a leap year.
My other condition is "Not evenly divisible by 100 but evenly divisible by 4". This is applicable for years that are not centenaries..
If none of the above conditions is satisfied, then it is a common year
Kandy
_________________
Try and Try again…You will succeed atlast!!
_________________
Try and Try again…You will succeed atlast!!
-
- Participant
- Posts: 597
- Joined: Fri Apr 29, 2005 6:19 am
- Location: Singapore