Page 1 of 1

Increment year by one

Posted: Tue Oct 18, 2011 10:00 am
by pdntsap
Hello,

We have a field stored as Date type in the following format: 10/18/11. I am looking for options to increase the year by one (to 10/18/12). Any suggestions are highly appreciated.

Also, is my following understanding of DaysSinceFromDate() function right.

Code: Select all

DaysSinceFromDate(10/17/12, 10/18/12)
will return one. Is that right?

Thanks.

Posted: Tue Oct 18, 2011 11:10 am
by MarkB
There are multiple ways to add a year - here is one way (in the example I am just using CurrentDate() ):

Code: Select all

StringToDate( YearFromDate(CurrentDate())+1:'-':MonthFromDate(CurrentDate()):'-':MonthDayFromDate(CurrentDate()))
As to your example, no - it will return a -1. DaysSinceFromDate uses (source date,given date) and returns the days from the given date to the source date, and the 17th was one day earlier (-1) from the 18th. In order to get 1, you would have to reverse the dates.

Posted: Tue Oct 18, 2011 11:48 am
by pdntsap
Thanks Mark.

Posted: Tue Oct 18, 2011 2:58 pm
by chulett
As noted, multiple ways... do you need to worry about the year after a leap year?

Posted: Wed Oct 19, 2011 6:54 am
by pdntsap
I do not need to worry about the year after leap year. I compare two dates (to and fro) and if the fro date is less than the to date, then increase the year by one on the fro date. Thanks.

Posted: Wed Oct 19, 2011 7:21 am
by MarkB
pdntsap wrote:I do not need to worry about the year after leap year. I compare two dates (to and fro) and if the fro date is less than the to date, then increase the year by one on the fro date. Thanks.
But as Craig noted, if you are doing as suggested, you really SHOULD address the leap year issue. If your from date is 02-29-2012 and you increase the year by one, you'll get 02-29-2013, which doesn't exist. Not a good thing. :cry:

You should check if the from date is 02-29; if it is, you'll need to change it to 02-28 before adding a year.

Posted: Wed Oct 19, 2011 7:37 am
by pdntsap
That is true. I guess I need to implement the leap year logic as well. Thanks.