Saturday, March 28, 2015

Calculate date from date picker inside InfoPath forms in SharePoint 2013




In this post we will discuss how to calculate date from a date picker inside a InfoPath forms in SharePoint 2013.

Recently I got a requirement to calculate some date based on a dropdown selected value. Like I have a dropdownlist which contains value like Yearly, half yearly, quarterly, months, week like that. And there is a date picker where user can select a date. If a user select a date from the date picker and then s/he select yearly from the dropdown then anther text should be populate with adding a year. So if user select 28-03-2015 then it should autopopulat 28-03-2016. Like that it should do for quarterly, monthly etc.

You may like to see:
- Format Date in InfoPath form in SharePoint 2013

- Alternative way to promote people picker in infopath form library in SharePoint 2013

- Deploy infopath form using PowerShell or STSADM command

We did not got any proper formula to do this inside the SharePoint infopath form. So we did that through client side code in the page. We have added a script editor webpart in the page and wrote the following code to do the calculation in the page.

Here ctl00_ctl29_g_0d2d6eff_f266_41bf_b14f_ce80bdc728ed_FormControl0_V1_I1_T11 is the id of the calendar control which user selecting.

<script type="text/javascript">
function test(){
var x = 12;
var CurrentDate = new Date();
CurrentDate.setMonth(CurrentDate.getMonth() + x);
alert(CurrentDate);
var d1=document.getElementById("ctl00_ctl29_g_0d2d6eff_f266_41bf_b14f_ce80bdc728ed_FormControl0_V1_I1_T11").value;
var date = new Date(d1);
date.setMonth(date.getMonth() + x);
alert(date);
}
</script>
<input onclick="test()" type="button" value="Alert value in form control"/>


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Favorites More