Expand my Community achievements bar.

add 365 or 366 days to selected date

Avatar

Level 5

Hi All.

I have form with two date field. The field EffectDate used  to enter random date and second is ExpDate to get date on one year late. How to get correct date on ExpDate field if differeence between dates possible 365 or 366 days. I will appreciate for brief explaination with sample.

Thanks.

27 Replies

Avatar

Former Community Member

I have a sample that shows the technique using FormCalc. Note that Date manipulation is much easier in FormCalc than in Javascript.

Paul

Avatar

Level 5

Hi Paul.

Thanks for replay.How to get correct date if difference between dates is 366 days?

Thanks.

Avatar

Former Community Member

You can see in the sample that the current date must be changed in to a format where we can do Date arithmetic. So the Date2Num function turns the date into th enumber of milliseconds from a point in time (called an epoch). Then we can add the 365 * 24*60* 60 milliseconds to the number. Now we can use the Num2Date function to get a date back.

paul

Avatar

Level 5

Hi Paul,

I modified code

TextField1.rawValue = TextField1.rawValue + 365

to

TextField1.rawValue = TextField1.rawValue + 365 * 24 * 60 * 60

I enter in EffectDate 08/30/2010 and get in ExpDate field wrong date like 4/19/8353. How to fix it? How to make ExpDate field empty if EffecDate is empty?

Thanks.

Avatar

Former Community Member

You cannot just add numbers to a date ...it makes no sense. If you look at the sample I sent you on the Button click event there is code that will change the date to the number of milliseconds from the epoch. Then you can add your 365 days....then you will need to turn the number back to a date before displaying it.

Paul

Avatar

Level 5

Hi Pual, Thanks for your replays.

I'm thinking for that calculation need to insert logic code. What I mean. If we check number of days in a year we will know that year is leap year or not. And then to perform some math operation. Can you show how it to do in my case?

Thanks.

Avatar

Former Community Member

If you are going to simply add a year to the current date then why does it matter whether it is a leap year or not. If you add 1 year to Aug 30, 2010 then shoudln't it be Aug 30, 2011?

Paul

Avatar

Level 5

Hi Paul,

If we have a deal with Aug 30, 2010 and  Aug 30, 2011 then difference 365 days and you are right. But what about Aug 30, 2011 and Aug 30, 2012. In that case difference is 366 days. Because in February 2012 has 29 days and February 2011 has 28 days. That is problem. If you know how it fix I will appreciate you.

Thanks.

Avatar

Former Community Member

So why not just add one to the year ...we can break the date into component parts and get the year back then add 1 and then reassemble the date

. Does that make sense? So whether it is a leap year or not the correct answer will always be the same date (Aug 30) + the year + 1.....makes sense?

Paul

Avatar

Former Community Member

I figured a sampel woudl be easier to explain ....have a look at the code in the add 1 year button.

paul

Avatar

Level 5

That is exactly what I said early. We need a logic statment to check amount of days in a year and if leap year add 366 if not so add 365.

Thanks.

Avatar

Former Community Member

If you break the date apart then adding 1 to the year should be all we need to do.

Paul

Avatar

Level 5

Hi Paul,

Is it possible to format date field to MM/DD/YYYY? If yes, how to do?

Thanks.

Avatar

Former Community Member

In the Date field on the Object palette - select the Value tab and click on the Validation Pattern tab. Now click on the Display tab and choose the appropriate style that you want. You shoudl end with a pattern that looks like this:

date{MM/DD/YYYY}

Note that you will have to change the code from the other sample as it uses a different format.

Paul

Avatar

Level 5

I know yet how to change date format for object. Can you suggest how to change the code to get correct result?

Thanks.

Avatar

Level 5

How to set up tool tip if type value of EndDate field is Read Only.

Thanks.

Avatar

Former Community Member

The tooltips will only come up in a field where you can enter data into the field. A readOnly field does not allow that so the tooltip will not show.

Paul

Avatar

Level 5

How to remove any tooltip if field is Read Only? Now the tooltip of read only field display like form1[0].#subform[0].EndDate[0]. How to remove it?

Thanks.

Avatar

Former Community Member

If the field is readOnly then the tooltip will not display ....are you seeing a tooltip on a ReadOnly field? The cursor shoudl stay as a hand and the field cannot be selected. Are you seeing different behaviour?

Paul