我将如何在C#中获得准确的时间跨度日期时间 [英] How I will get exact time timespan datetime in C#

查看:128
本文介绍了我将如何在C#中获得准确的时间跨度日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从失败的时间到恢复时间的持续时间,当我尝试恢复时间 - 失败的时间,它在时间跨度提交的确切时间。但当它进入span.Totalminutes时值是变化。

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.

var errorPeriod = span != TimeSpan.Zero ? Math.Round(span.TotalMinutes, 2) + " mins" : "Immediately";



i想要确切的时间。我尝试了另一种方式


i want the exact time .i tried another way

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:

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, );

}

推荐答案

您的问题不明确。如果没有示例恢复日期和失败日期,我不知道出了什么问题。但是现在看来你需要查看TimeSpan文档。



TimeSpan Structure(System) [ ^ ]



如果你想澄清你的问题使它有意义,随意使用改进问题。否则,我建议您查看TimeSpan结构文档,以便使用.Minutes和.TotalMinutes。
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.


这篇关于我将如何在C#中获得准确的时间跨度日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆