I have been working on a project that i received some great help from users here before. I am trying to subtract times that are formatted in a String.
Here is the code that i am using that works like a champ with one exception. It does not matter if i subtract value2 from value1 or value1 from value2 the answer is always the same. I am looking to have it provide me with a negative number if the value being subtracted is greater than the number being subtracted from.
Dim cell2Value = "08:01:05.900" 'dgvConvert(3, 1).Value.ToString
Dim cell1Value = "08:01:50.000" 'dgvConvert(3, 2).Value.ToString
Dim value2 = TimeSpan.Parse(cell2Value)
Dim value1 = TimeSpan.Parse(cell1Value)
TextBox1.Text = (value2 - value1).ToString("hh\:mm\:ss\.fff")
You could use a check to see if the you have a negative time, and then pick a different format.
Dim cell2Value As String = "08:01:05.100" 'dgvConvert(3, 1).Value.ToString Dim cell1Value As String = "08:01:55.123" 'dgvConvert(3, 2).Value.ToString Dim value2 As TimeSpan = TimeSpan.Parse(cell2Value) Dim value1 As TimeSpan = TimeSpan.Parse(cell1Value) Dim diff As TimeSpan = value2 - value1 Dim s As String = If(diff.TotalMilliseconds >= 0, diff.ToString("hh\:mm\:ss\.fff"), diff.ToString("\-hh\:mm\:ss\.fff"))