1、字符串转换成日期
如ruzhuDate = "2015-12-16"
DateTime ruzhuDate = Convert.ToDateTime(dgv2.Rows[i].Cells["ruzhuDate"].Value.ToString());
string banchuDate = dgv2.Rows[i].Cells["banchuDate"].Value.ToString() == string.Empty ? "" :Convert.ToString(dgv2.Rows[i].Cells["banchuDate"].Value);
DateTime date = Convert.ToDateTime(banchuDate)
2、两个日期值比较大小
如qsrq = "2015-12-30"
DateTime.Compare(ruzhuDate, qsrq) < 0
DateTime.Compare(t1,t2),方法获取一个数字,果之小于0,则t1<t2,大于0,则t1>t2, 等于0,则t1=t2
2、两个日期相差天数
如qsrq = "2015-11-25"
jsrq = "2015-12-24"
System.TimeSpan diff = jsrq.Subtract(qsrq);
dgv2.Rows[i].Cells["dayQty"].Value = diff.Days.ToString();
C#中计算2个时间之间的差(可以计算到时分秒);
参考代码如下:
label1.Text =
"2012-1-1 15:36:05"
;
label2.Text =
"2012-2-1 20:16:35"
;
DateTime d1 =
new
DateTime(2012,1,1,15,36,05);
DateTime d2 =
new
DateTime(2012,2,1,20,16,35);
TimeSpan d3 = d2.Subtract(d1);
label3.Text =
"相差:"
+d3.Days.ToString()+
"天"
+d3.Hours.ToString()+
"小时"
+d3.Minutes.ToString()+
"分钟"
+d3.Seconds.ToString()+
"秒"
;