using Microsoft.Office.InfoPath; using System; using System.Xml; using System.Xml.XPath; using System.Collections.Generic; using Microsoft.SharePoint; namespace Test { public partial class FormCode { public object res { get { return FormState["res"]; } set { FormState["res"] = value; } } public object result { get { return FormState["result"]; } set { FormState["result"] = value; } } public void InternalStartup() { EventManager.XmlEvents["/my:myFields/my:group1/my:EndDT"].Changed += new XmlChangedEventHandler(EndDT_Changed); } public void EndDT_Changed(object sender, XmlEventArgs e) { // Write your code here to change the main data source. if (e.Operation == XmlOperation.ValueChange) { //Retrieve the values for the start and end time string startTimeValue = e.Site.SelectSingleNode("../my:StartDT", NamespaceManager).Value; string endTimeValue = e.Site.SelectSingleNode("../my:EndDT", NamespaceManager).Value; if (!String.IsNullOrEmpty(startTimeValue) && !String.IsNullOrEmpty(endTimeValue)) { //Convert the time values to DateTime objects //TimeSpan startTime = TimeSpan.Parse(startTimeValue); //TimeSpan endTime = TimeSpan.Parse(endTimeValue); DateTime startTime; DateTime endTime; //string date1 = "2015-03-23 12:34:56"; //string date2 = "2015-03-23 12:34:56"; try { startTime = DateTime.Parse(startTimeValue); endTime = DateTime.Parse(endTimeValue); //int startTime2 = startTime.Date.Hour; //int endTime2 = endTime.Date.Hour; //int a = startTime.Hour; //int b = endTime.Hour; TimeSpan diff = endTime.Subtract(startTime); string dateInput = diff.ToString(); int day = 0, hour = 0, minutes = 0; try { List<string> dateSplit = new List<string>(dateInput.Split('.', ':')); if (dateSplit.Count == 4) { for (int i = 2; i < dateSplit.Count; i++) // Loop with for. { day = int.Parse(dateSplit[0].ToString()); hour = int.Parse(dateSplit[1].ToString()); minutes = int.Parse(dateSplit[2].ToString()); } res = (int)day * 24; result = (string)res + hour + ":" + minutes; } else if (dateSplit.Count == 3) { for (int i = 2; i < dateSplit.Count; i++) // Loop with for. { day = int.Parse(dateSplit[0].ToString()); hour = int.Parse(dateSplit[1].ToString()); minutes = int.Parse(dateSplit[2].ToString()); } res = day; result = res + ":" + hour; } } catch (Exception ek) { } } catch (FormatException ex) { //Retrieve the time difference field node and remove its nil attribute XPathNavigator nodeNav = e.Site.SelectSingleNode("../my:Duration", NamespaceManager); if (nodeNav.MoveToAttribute("nil", NamespaceManager.LookupNamespace("xsi"))) nodeNav.DeleteSelf(); //Retrieve and display the time difference e.Site.SelectSingleNode("../my:Duration", NamespaceManager).SetValue(result.ToString()); //((TimeSpan)endTime.Subtract(startTime)).Hours.ToString()); //(diff.ToString()); //((TimeSpan)endTime.Subtract(startTime)).Hours.ToString()); } } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)