Page 1 of 1

How to add one year to a given date

Posted: Sat Jun 09, 2007 9:30 am
by vijaydev
Pls. tell me any function is there to add one year to a given date

Thanks in advance


Vijay

Posted: Sat Jun 09, 2007 10:07 am
by chulett
No. You'd need to have a clear understanding what precisely 'adding a year' to any given date means and then write your own. You'd also need to know what kind of dates you need to handle - any format? One particular format? What format do you return the result in? Things to think about.

If it is as simple as incrementing the year portion of the date, then one way would be to substring it off, increment, and then concatenate it back together.

Posted: Sat Jun 09, 2007 10:23 am
by vijaydev
"YYYY-MM-DD" i need to add one year to this the out put format is same

Eg: 2007-06-09 i need to get 2008-06-08

2007-06-01 then i need 2007-05-31

Posted: Sat Jun 09, 2007 11:12 am
by chulett
So... you just want to add 365 days to the date? Is that what this boils down to? :?

Use IConv to convert the date to internal format, add 365 and then convert back to your external format with OConv.

Posted: Sat Jun 09, 2007 6:36 pm
by ray.wurlod
Adding one to the year portion always works unless the starting date is Feb 29, in which case you also need to subtract one from the day number.

Or you could download a function from here where you will find both AddYears (to date) and AddYearsToTimestamp functions among others. Each has its own documentation on the General page, including instructions as to how it is to be used, what format arguments are expected to be in, and so on.

Posted: Sat Jun 09, 2007 6:42 pm
by chulett
Ah yes... the Patent Pending Ray Wurlod Date Routines. Excellent stuff. :wink:

Posted: Sat Jun 09, 2007 10:39 pm
by vijaydev
Any one help in this regard

Posted: Sat Jun 09, 2007 11:56 pm
by chulett
Um... Hello? McFly! What's wrong with the help you've gotten so far? :evil:

If we're way off the mark, say so. And if so, you could start off by explaining why you "need" to get 2008-06-08 when you add "1 year" to 2007-06-09. I think most people would assume the date should be 2008-06-09. Explain the rule you need to implement.

Re: How to add one year to a given date

Posted: Sun Jun 10, 2007 5:12 am
by ray.wurlod
vijaydev wrote:Pls. tell me any function is there to add one year to a given date
Yes there is.

I gave you a link whence you can download a whole suite of them.

If you want more help than that, such as designing your ETL, that counts as consulting, and will cost you.

Posted: Sun Jun 10, 2007 11:30 pm
by vijaydev
What about Leap year


Regards,
Vijay

Posted: Mon Jun 11, 2007 1:56 am
by rafik2k
for leap year u need to check year first for leap year like following:

Code: Select all

if (mod(year,4)=0 or mod(year,100)=0 or mod(year,400)=0) then
  // for leap year
  add 366 days
else
  // not leap year
 add 365 days

Posted: Mon Jun 11, 2007 5:05 am
by ray.wurlod
You haven't even looked at the routines I suggested, have you?
:roll:

Leap years actually do not matter in an AddYears() function unless the current (base) date is February 29th or the Pope has adjusted the calendar in the interim.

Posted: Mon Jun 11, 2007 6:55 am
by chulett
ray.wurlod wrote:or the Pope has adjusted the calendar in the interim.

Yah, gotta watch out for them Popes. They does stuffs like that every now and then. :wink:

Posted: Mon Jun 11, 2007 3:14 pm
by ray.wurlod
Pope Gregory shortened October of A.D. 1582 by ten days. Thursday, October 4, 1582 (Julian) was followed immediately by Friday, October 15, 1582 - see this site for details.

My routine handles this situation, because the underlying "UniVerse" date handling functions are aware of it.

But it would appear vijaydev is not interested in a supplied, totally correct, solution.