Click here to Skip to main content
15,991,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want time duration from failed time to recovery time when i tried recovery time - failed time , its coming exact time in timespan filed . but when its goes into span.Totalminutes the value is change.
C#
var errorPeriod = span != TimeSpan.Zero ? Math.Round(span.TotalMinutes, 2) + " mins" : "Immediately";

i want the exact time .i tried another way
C#
var errorPeriod = span != TimeSpan.Zero ? Math.Max(span.Minutes, 2)+"."+ Math.Max(span.Seconds, 2) + " mins" : "Immediately";

but its subtracts mintues & second diffrent diffrent so i am getting wrong answers
but i am unable to find the right way .

What I have tried:

C#
if (node.ChildNodes.Item(4).InnerText.Equals(""))
                    {                       
                        string failed = node.ChildNodes.Item(9).InnerText;
                        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("recovered");
                        //node.ChildNodes.Item(6).InnerText = DateTime.Now.ToString();
                        string recovered = node.ChildNodes.Item(6).InnerText;
                       TimeSpan span = (failed != string.Empty && recovered != string.Empty) ? DateTime.Parse(recovered).Subtract(DateTime.Parse(failed)) : TimeSpan.Zero;
                       var errorPeriod = span != TimeSpan.Zero ? Math.Round(span.TotalMinutes, 2) + " mins" : "Immediately";

                        var errorPeriod = span != TimeSpan.Zero ? Math.Max(span.Minutes, 2)+"."+ Math.Max(span.Seconds, 2) + " mins" : "Immediately";
                                            
                        sbRecoveryMailBody.AppendFormat("{0}{1}{2}", failed, recovered, errorPeriod, );
}
Posted
Updated 23-Sep-16 4:55am
v4
Comments
[no name] 23-Sep-16 9:58am    
Debug your code and figure it out. We would have no idea what the value of "failed" and "recovered" is, or what "the value is change" means or "getting wrong answers".

1 solution

Your question is not clear. Without an example recovered date and failed date I don't know what is wrong. But for now it seems you need to review the TimeSpan documentation.

TimeSpan Structure (System)[^]

If you'd like to clarify your issue so that it makes sense, feel free to use improve question. Otherwise, I'd encourage you to review the TimeSpan structure documentation so the use of .Minutes and .TotalMinutes will make sense.
 
Share this answer
 

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