Date Picker Calculation

T

tmons

I have two date picker fields - an Effective Date Start and an Effective Date
End. I'd like the end date to automatically populate one year from the date
entered in the start date. Can you give me the correct formula for this?
 
F

Franck Dauché

Hi,

Let's build a little project to get you started:

create new IP project (with C# behind) using Visual Studio.
Drop 2 date picker controls on your page (you now have 2 fields in your
schema: field1 and field2).
For the purpose of this exercise, open you mayschema.xsd file and go to the
XML view.
remove the "nillable=true" lines to give:
<xsd:element name="field1" type="xsd:date"/>
<xsd:element name="field2" type="xsd:date"/>

Next, at the bottom of your FormCode.cs code page, copy the 2 functions
given to you in the blog:
private static DateTime Iso8601ToDate( string iso8601Date )
{
if( iso8601Date == null ) throw new ArgumentNullException( "iso8601Date" );
return DateTime.ParseExact( iso8601Date, "yyyy-MM-dd", null );
}
private static string DateToIso8601( DateTime dateTime )
{return dateTime.ToString( "yyyy-MM-dd" );}


Then, go to your IP form:
Right-click on the first Date Picker (field1), click on Data Validation,
select OnAfterChange from the event drop-down and click Edit.
Now insert the following code:
[InfoPathEventHandler(MatchPath="/my:myFields/my:field1",
EventType=InfoPathEventType.OnAfterChange)]
public void field1_OnAfterChange(DataDOMEvent e)
{
if (e.IsUndoRedo){return;}
IXMLDOMNode dateNode = thisXDocument.DOM.selectSingleNode( "//my:field2" );
try
{
DateTime dt = Iso8601ToDate( e.Source.text );
dt = dt.AddYears( 1 );
dateNode.text = DateToIso8601( dt );
}
catch( FormatException ) {}
}

run your form, pick 1 date in Date Picker 1, Date Picker 2 fills up with the
same date + 1 year...

Voila!

Hope that it helps you.

Franck Dauché
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top