Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I calculate how many days,hours,minutes,seconds will take repair of machine.

1. position - register machine error (format:
dd/MM/yyyy HH:mm:ss
)
2. position - repair this machine error.(format:
dd/MM/yyyy HH:mm:ss
)

now I would like calculate how many days,hours,minutes,seconds will take this repair of machine. (1.position - 2. position.

How can I do it?

Thank for advise

What I have tried:

_________________________________
Posted
Updated 14-Jan-19 21:27pm

Convert them to DateTime values using DateTime.TryParseExact[^], then subtract them. That will give you a TimeSpan[^] value that has the properties you need.
VB
Dim date1 As String = "30/12/2018 06:41:13"
Dim date2 As String = "02/01/2019 17:01:42"
Dim dt1 As DateTime

If Not DateTime.TryParseExact(date1, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, dt1) Then
    ... report problem to user ...
    Return
End If

Dim dt2 As DateTime

If Not DateTime.TryParseExact(date2, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, dt2) Then
    ... report problem to user ...
    Return
End If

Dim diff As TimeSpan = dt2 - dt1
Console.WriteLine(diff.Days)
Console.WriteLine(diff.Hours)
Console.WriteLine(diff.Minutes)
Console.WriteLine(diff.Seconds)
 
Share this answer
 
Please, read this first: Performing arithmetic operations with dates and times | Microsoft Docs[^]

VB.NET
Dim startTime As DateTime = DateTime.Now
Dim endTime As DateTime = DateTime.Now.AddDays(1).AddHours(5).AddMinutes(93).AddSeconds(75)
Dim span As TimeSpan = endTime.Subtract(startTime)
Console.WriteLine("Time Difference: {0} days, {1} hours, {2} minutes, {3} seconds", span.Days, span.Hours, span.Minutes, span.Seconds)

Result:
Time Difference: 1 days, 6 hours, 34 minutes, 15 seconds


Good luck!
 
Share this answer
 
v2
Comments
Member 13711215 15-Jan-19 4:32am    
How can I write result to label?
Maciej Los 15-Jan-19 4:43am    
Me.Label1.Text = String.Format("Time Difference: {0} days, {1} hours, {2} minutes, {3} seconds", span.Days, span.Hours, span.Minutes, span.Seconds)
Member 13711215 15-Jan-19 5:41am    
thank you
Maciej Los 15-Jan-19 5:46am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900