Can someone please help me to make this work?
I want to calculate the time between two dates in VB.NET like this:
startdate: 2011/12/30
enddate: 2011/12/31
Calculate: ? hour ? minute ? seconds
To calculate the number of days between two dates, you need to subtract the start date from the end date. If this crosses several years, you should calculate the number of full years. For the period left over, work out the number of months.
To calculate the number of hours between two dates we can simply subtract the two values and multiply by 24.
You Can try this
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 );
Output Like,
Time Difference (seconds): 15
Time Difference (minutes): 1
Time Difference (hours): 0
Time Difference (days): 0
And the VB.Net equivalent to the above:
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)
When you subtract 2 DateTime
s, you get a TimeSpan
struct that has all of those properties for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With