Convert TimeSpan.TotalMilliseconds to datetime and format it as hour:minute

孤街浪徒 提交于 2019-12-12 05:30:02

问题


I kinda confuse about the date time conversion

in my model i have defined

    public DateTime? Sent { get; set; }
    public DateTime? Reply { get; set; }
    public double ResponseTime { get; set; }

in linq part i am using

ResponseTime = (u.Reply - u.sent).TotalMilliseconds

which is not formated and displayed like this 979809803

I want to know how do i convert it to datetime format, and eventually will display the format as hour:minute, for instance 2:45 between the date sent and date reply.


回答1:


Subtracting a DateTime from a DateTime yields a TimeSpan. Have a look at the TimeSpan.ToString Method (String) to see how you can custom format the value.

You can change ResponseTime back to a TimeSpan and from there to a string.

TimeSpan ResponseTimeSpan = new TimeSpan(0, 0, 0, (int)ResponseTime);

string ResponseTimeDisplay = ResponseTimeSpan.ToString();



回答2:


Just return the TimeSpan and call .ToString("hh:mm").

TimeSpan.ToString

public TimeSpan ResponseTime { get; set; }

//usage in LINQ
ResponseTime = (u.Reply - u.Sent)

When displaying...

value.ResponseTime.ToString("hh:mm")


来源:https://stackoverflow.com/questions/13057928/convert-timespan-totalmilliseconds-to-datetime-and-format-it-as-hourminute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!