c# 计算两个日期之间相差的小时数
解决方案 »
- VS2010 SP1升级后项目里生成的exe文件,RDLC报表钻取功能,在升级之后不能用了 即使重新编译也不行
- 水印平铺问题?
- 急!多线程TCP/IP问题,求大家帮忙!
- 这样的验证码谁能识别,能给出代码谢之!
- DataGridView 中的一个cell(单元格)如何放两个控件,比如一个单元各种包含一个textbox一个命令按钮
- 此處delegate該如何理解啊?
- 请问如何改变listview的表头颜色?
- 火线求救各位大虾,谁知道在.net下,怎么用Neo来将XML文件生成C#文件吗?
- socket同步通讯,收到的数据无法进行字符串运算?
- 怎样检测一个url可用
- C# visio
- C#开发出的系统为什么复制到别的计算机上就不能用了呢?
先将两个字符型日期转换为DateTime类型,然后计算TimeSpan,再调用TotalHours属性。
ts.Hours就是你要的
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now.AddHours(-3).AddDays(-1);
TimeSpan ts = dt1.Subtract(dt2);
double hours = ts.TotalHours;//27.0
DateTime b = a.AddHours(5); int c =(b - a).Hours;DateTime有操作符运算 // Summary:
// Subtracts a specified date and time from another specified date and time
// and returns a time interval.
//
// Parameters:
// d1:
// A System.DateTime (the minuend).
//
// d2:
// A System.DateTime (the subtrahend).
//
// Returns:
// A System.TimeSpan that is the time interval between d1 and d2; that is, d1
// minus d2.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static TimeSpan operator -(DateTime d1, DateTime d2);
//
// Summary:
// Subtracts a specified time interval from a specified date and time and returns
// a new date and time.
//
// Parameters:
// d:
// A System.DateTime.
//
// t:
// A System.TimeSpan.
//
// Returns:
// A System.DateTime whose value is the value of d minus the value of t.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// The resulting System.DateTime is less than System.DateTime.MinValue or greater
// than System.DateTime.MaxValue.
public static DateTime operator -(DateTime d, TimeSpan t);
//
// Summary:
// Determines whether two specified instances of System.DateTime are not equal.
//
// Parameters:
// d1:
// A System.DateTime.
//
// d2:
// A System.DateTime.
//
// Returns:
// true if d1 and d2 do not represent the same date and time; otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator !=(DateTime d1, DateTime d2);
//
// Summary:
// Adds a specified time interval to a specified date and time, yielding a new
// date and time.
//
// Parameters:
// d:
// A System.DateTime.
//
// t:
// A System.TimeSpan.
//
// Returns:
// A System.DateTime that is the sum of the values of d and t.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// The resulting System.DateTime is less than System.DateTime.MinValue or greater
// than System.DateTime.MaxValue.
public static DateTime operator +(DateTime d, TimeSpan t);
//
// Summary:
// Determines whether one specified System.DateTime is less than another specified
// System.DateTime.
//
// Parameters:
// t1:
// A System.DateTime.
//
// t2:
// A System.DateTime.
//
// Returns:
// true if t1 is less than t2; otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator <(DateTime t1, DateTime t2);
//
// Summary:
// Determines whether one specified System.DateTime is less than or equal to
// another specified System.DateTime.
//
// Parameters:
// t1:
// A System.DateTime.
//
// t2:
// A System.DateTime.
//
// Returns:
// true if t1 is less than or equal to t2; otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator <=(DateTime t1, DateTime t2);
//
// Summary:
// Determines whether two specified instances of System.DateTime are equal.
//
// Parameters:
// d1:
// A System.DateTime.
//
// d2:
// A System.DateTime.
//
// Returns:
// true if d1 and d2 represent the same date and time; otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator ==(DateTime d1, DateTime d2);
//
// Summary:
// Determines whether one specified System.DateTime is greater than another
// specified System.DateTime.
//
// Parameters:
// t1:
// A System.DateTime.
//
// t2:
// A System.DateTime.
//
// Returns:
// true if t1 is greater than t2; otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator >(DateTime t1, DateTime t2);
//
// Summary:
// Determines whether one specified System.DateTime is greater than or equal
// to another specified System.DateTime.
//
// Parameters:
// t1:
// A System.DateTime.
//
// t2:
// A System.DateTime.
//
// Returns:
// true if t1 is greater than or equal to t2; otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator >=(DateTime t1, DateTime t2);
Timespan = time1-time2
用timespan函数 变量 得到 两个时间的时差然后格式化 d 就得到了 或者直接取day