如何计算两次之间的差? [英] How to caluculate the difference between two times?

查看:95
本文介绍了如何计算两次之间的差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在像快速解答"网站一样开发问题论坛.我要

计算当前提问时间之间的时差发表

时间如下:

发布于:1小时44秒前


请给我有关如何执行此操作的想法.



在此先感谢.


问候,

普拉萨德·雷迪(Prasad Reddy). M

Hi,

I am developing Question Forum like this Quick Answers site. I want to

calculate the time difference between the Present Time to Question Posted

Time as follows:

Posted on: 1 hour 44 Sec ago


Please give me idea on how to do this.



Thanks in Advance.


Regards,

Prasad Reddy. M

推荐答案

.NET框架轻轻地为您提供TimeSpan结构[
The .NET framework gently provides you the TimeSpan struct [^].
:)


您可以使用TimeSpan

这是它的一个例子

you can use TimeSpan

here is one example of it

public string getFormattedTimeFromSecond(double second)
    {

        TimeSpan t = TimeSpan.FromSeconds(second);

        string formatedTime = string.Format("{0:D2}H:{1:D2}M:{2:D2}S",
                                t.Hours,
                                t.Minutes,
                                t.Seconds);

        return formatedTime;
}




您可以使用以下代码:

Hi,

you can use the following code:

DateTime currentDate = DateTime.Now;
DateTime postedDate = DateTime.Now.AddHours(-2);
TimeSpan ts = postedDate.Subtract (currentDate);



然后您可以使用TimeSpan来显示所需的文本;

问候,
贾米尔(Jamil)



then you can use the TimeSpan to display the text you want;

Regards,
Jamil


这篇关于如何计算两次之间的差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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