今天遇到一问题,计算两个时间的时间差,看网上的写法较为复杂,找到个简单点的,记录下作为自己的总结。

  关键函数:

DateTime.Subtract

  函数解释:

从此实例中减去指定的日期和时间,返回一个时间间隔,它等于此实例所表示的日期和时间减去 value 所表示的日期和时间。

测试代码:

        /// <summary>
/// 测试时间差
/// </summary>
[TestMethod]
public void TestTimeSubTract()
{
DateTime time1 = DateTime.Now;
DateTime time2 = DateTime.Now.AddHours(-);
//间隔时间
TimeSpan subTract= time2.Subtract(time1);
//间隔小时数
int subHours = subTract.Hours;
//两个时间的间隔小时数
Assert.AreEqual(subHours, -);
}

C#两个时间的时间差的方法的更多相关文章

  1. C#计算两个时间的时间差,精确到年月日时分秒

    喏,计算两个时间的时间差,精确到年月日时分秒 看起来比较笨的方法了,不知道有没有改进 DateTime d1 = new DateTime(2016, 4, 1, 0, 0, 0); DateTime ...

  2. JS比较两个时间的时间差

    /** * 比较两个时间的时间差 * @param startTime 开始时间 * @param endTime 结束时间 * @demo compareTime(new Date('2019-12 ...

  3. C#实现两个时间相减的方法

    脚本之家看到的,关于两个时间差值的获取 http://www.jb51.net/article/60177.htm using System; using System.Collections.Gen ...

  4. JavaScript获取两个时间的时间差

    <html><head><title>JavaScript计算两个时间差</title><meta http-equiv="conten ...

  5. iOS-----------计算两个时间的时间差

    UIButton * nameButton = [UIButton buttonWithType:UIButtonTypeCustom]; nameButton.frame = CGRectMake( ...

  6. php如何判断两个时间的时间差

    $time1=2011-11-11 11:11:11$time2=2016-12-10 16:58:13 代码: if(abs(strtotime($time2)-strtotime($time1)) ...

  7. c# 计算两个时间的时间差

    //计算2个日期之间的天数差 DateTime dt1 = Convert.ToDateTime("2007-8-1"); DateTime dt2 = Convert.ToDat ...

  8. iOS计算两个时间的时间差

    + (long)calculteHourL:(NSDate *)endDate startDate:(NSDate *)startDate { NSCalendar *cal = [NSCalenda ...

  9. js前台计算两个日期的间隔时间(时间差)

    在后台传来两个时间字段,从中解析出两个字符串类型的日期格式 需要在前台解析出两个时间的间隔 这里采用获取两个日期的时间戳进行计算 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时 ...

随机推荐

  1. 【转】Linux下tar.xz结尾的文件的解压方法--不错

    原文网址:http://blog.csdn.net/silvervi/article/details/6325698 今天尝试编译内核,下载到了一份tar.xz结尾的压缩文件,网上解决方法比较少,不过 ...

  2. android网址

    1. 优麒麟手机助手 https://github.com/ubuntu-kylin/phone_manager 2. http://forum.xda-developers.com/index.ph ...

  3. C++小知识之sprintf用法

    sprintf   字串格式化命令,主要功能是把格式化的数据写入某个字符串中.sprintf 是个变参函数,使用时经常出问题,而且只要出问题通常就是能导致程序崩溃的内存访问错误,但好在由sprintf ...

  4. hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)

    Problem Description   You live in a village but work in another village. You decided to follow the s ...

  5. textChanged(*)重点

    # -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui class MyDialog(QtGui.QDialog): de ...

  6. 给SharePoint页面加入自己定义页脚Custom footer

    给SharePoint页面加入自己定义页脚Custom footer         在公司做站点设计项目时,须要在页面上加入页脚.         非常多人都把页脚忽视了,认为没什么多大用处,事实上 ...

  7. Linux下redis的安装及用法

    1.下载源代码包redis-2.8.21.tar.gz,并将其上传到指定文件夹/urs/src,然后对其进行解压: [root@Slave1pc src]# tar -xvf redis-2.8.21 ...

  8. char值码对应大全

    Char("0") 为0的字符Char("1") Char("2") Char("3") Char("4&qu ...

  9. ASP.NET的Application简介1

    ASP.NET中的Application 1. Application是用于保存所有用户共有的信息.在ASP时代,如果要保存的数据在应用程序生存期内不会或者很少改变,那么使用Application是理 ...

  10. hdu 1085 Holding Bin-Laden Captive!

    Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...