计算两个日期之间的时间?

时间:2011-12-31 13:04:27

标签: vb.net vb.net-2010

有人可以帮我做这项工作吗?

我想在VB.NET中计算两个日期之间的时间,如下所示:

startdate: 2011/12/30  
enddate: 2011/12/31  

计算:?小时?一分钟?秒

2 个答案:

答案 0 :(得分:17)

你可以尝试这个

DateTime startTime = DateTime.Now;

DateTime endTime = DateTime.Now.AddSeconds( 75 );

TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );

输出赞,

Time Difference (seconds): 15
Time Difference (minutes): 1
Time Difference (hours): 0
Time Difference (days): 0

VB.Net等同于上面的内容:

Dim startTime As DateTime = DateTime.Now

Dim endTime As DateTime = DateTime.Now.AddSeconds(75)

Dim span As TimeSpan = endTime.Subtract(startTime)
Console.WriteLine("Time Difference (seconds): " + span.Seconds)
Console.WriteLine("Time Difference (minutes): " + span.Minutes)
Console.WriteLine("Time Difference (hours): " + span.Hours)
Console.WriteLine("Time Difference (days): " + span.Days)

答案 1 :(得分:5)

当你减去2 DateTime时,你会得到一个TimeSpan结构,其中包含所有这些属性。