Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm new to Infopath. I need a help on it.

I have an Infopath 2013 form that contains three fields:
Two datetime field using DateTime picker and the 3rd contains the text field need to find the difference between in hours

How I can make the difference between the two so that the display will be like this:

Date Time1 : 10/27/2015 05:59:53 PM
Date Time2 2: 10/28/2015 05:59:56 PM
Difference : 1 days, 0 hours, 0 minutes and 3 seconds.

Please help me to get this output using custom code or without code.

Thank you in advance for your answer
Posted

1 solution

The result of the subtraction of two DateTime values yields a TimeSpan value.
You can then use this TimeSpan for your requirement. This way:
C#
TimeSpan ts = Time2 - Time1;
string displayedResult = string.Format("{0} days, {1} hours, {2} minutes and {3} seconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);

Hope this helps.
 
Share this answer
 
Comments
v.jobukamaraj 28-Oct-15 8:48am    
I write this code in Validating event but it's not recognizing the Startdatetime and Enddatetime field which is present in the Infopath form

public void resolutiontime_Validating(object sender, XmlValidatingEventArgs e)
{
// Write your code here.
TimeSpan ts = EndDatetime - StartDatetime;
string resolutiontime = string.Format("{0} days, {1} hours, {2} minutes and {3} seconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
}

Let me know what should I need to do.

Receiving Error like : Error The name 'EndDatetime' does not exist in the current context
Receiving Error like : Error The name 'StartDatetime' does not exist in the current context
phil.o 28-Oct-15 8:52am    
I don't have enough contextual information to answer that. You need to find the correct variable accessor from where you are trying to get the values of EndDateTime and StartDateTime variables. Is there any intellisense in InfoPath?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900