打算写个程序,内容很简单就是想计算下偶家小公主的年龄,当然偶水平也简单找了网上代码才搞定,分享代码如下。@VCOO

C#计算两个日期的间隔:

CS代码如下:

[csharp]
private void Form1_Load(object sender, EventArgs e)
{
DateTime d1 = DateTime.Parse("2012-09-07 11:05");
DateTime d2 = DateTime.Now;

this.lblAge.Text = this.DateDiff(d1, d2);
}

/// <summary>
/// 计算两个日期间隔的时间
/// </summary>
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
string dateDiff = null;

TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();

dateDiff = "婧瑶年龄:" + ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟";

return dateDiff;
}[/csharp]

发表评论

/ 快捷键:Ctrl+Enter
加载中……