Solved

VB.net - calculate time difference

Posted on 2013-06-04
4
483 Views
Last Modified: 2013-06-05
VB.net

Let say I have the two date/times:
6/4/2013 5:46:41 PM
6/4/2013 5:50:03 PM
=  3:22 seconds

6/4/2013 5:54:29 PM
6/4/2013 5:55:30 PM
= 0:59 seconds

How can I can calculate how many minutes and seconds between the two times?
0
Comment
Question by:keith1001
4 Comments
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 39220814
Dateadd would do the trick.

If minutes = 0 then datrdiff s

HTH,

Kent
0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 250 total points
ID: 39220878
        Dim d1 As Date = Date.Parse("6/4/2013 5:46:41 PM")
        Dim d2 As Date = Date.Parse("6/4/2013 6:50:03 PM")

        Dim ts As TimeSpan = d2.Subtract(d1)

        Dim s1 As String = ts.ToString  'hh:mm:ss
        Dim s2 As String = String.Format("{0}:{1}", Math.Floor(ts.TotalMinutes), ts.Seconds.ToString("00")) 'mm:ss

        MessageBox.Show(String.Format("{0}{1}{2}", s1, Environment.NewLine, s2))

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39220879
Subtracting two dates in .NET yields a TimeSpan instance. You can interrogate the TotalSeconds property of that instance.

e.g.

Dim firstDate As DateTime = DateTime.Parse("6/4/2013 5:46:41 PM")
Dim secondDate As DateTime = DateTime.Parse("6/4/2013 5:50:03 PM")

Dim diff As TimeSpan = (secondDate - firstDate)

Console.WriteLine(diff.TotalSeconds)

Open in new window

0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39222002
hi.. 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
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - Get ColumnNames of SQL Table 4 15
ASP/VB email question 4 46
vb.net 1 month apart 11 35
ASP.NET/VB: Convert Date and Time to YYYY-MM-DDTHH:MM:SS 3 14
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question