Calculate only hours minutes seconds from TimeSpan - vb.net

I have a 7 string in this format 00:00:00
Im trying to use timespan to calculate those strings
the problem is it counts it with days
Dim l1 As String = "14:32:45"
Dim l2 As String = "07:43:13"
Dim l3 As String = "00:00:00"
Dim l4 As String = "00:00:00"
Dim l5 As String = "18:34:54"
Dim l6 As String = "00:00:00"
Dim l7 As String = "12:00:34"
Dim ts1 As TimeSpan = TimeSpan.Parse(l1)
Dim ts2 As TimeSpan = TimeSpan.Parse(l2)
Dim ts3 As TimeSpan = TimeSpan.Parse(l3)
Dim ts4 As TimeSpan = TimeSpan.Parse(l4)
Dim ts5 As TimeSpan = TimeSpan.Parse(l5)
Dim ts6 As TimeSpan = TimeSpan.Parse(l6)
Dim ts7 As TimeSpan = TimeSpan.Parse(l7)
Dim ttsum As New TimeSpan
ttsum = ts1 + ts2 + ts3 + ts4 + ts5 + ts6 + ts7
MsgBox(ttsum.ToString)
Output : 2.04:51:26
Desired output : 52:51:26

That's the way a TimeSpan works. If you don't want the output of TimeSpan.ToString then don't use it. You can do your own calculation of the number of hours and use that:
MessageBox.Show($"{Convert.ToInt32(ttsum.TotalHours)}:{ttsum.Minutes:00}:{ttsum.Seconds:00}")

Related

im trying to find the gap between 2 numbers

I'm having trouble finding the gap between 2 numbers for a parking charge. I tried this:
'time for entry
Dim entered As String = txtHourEnter.Text + ":" + txtMinEnter.Text
Dim time As DateTime
Dim display As String = "Invalid entry"
If DateTime.TryParse(entered, time) Then
display = time.ToString("h:mm tt")
End If
lblTimeIn.Text = display
'time for exited
Dim exited As String = txtHourExit.Text + ":" + txtMinExit.Text
Dim out As DateTime
Dim display2 As String = "invalid entry"
If DateTime.TryParse(exited, out) Then
display2 = out.ToString("h:mm tt")
End If
lblTimeOut.Text = display2
'parking time
Dim parkingtime As String = (display - display2)
lblParkingTime.Text = parkingtime
But I get this error:
The OP forgot to include the error message. How embarrassing for them :(
The code was trying to subtract the strings, instead of the timestamps. You need to get actual datetime values and subtract those.
Dim hourEnter As Integer = Int32.Parse(txtHourEnter.Text)
Dim minuteEnter As Integer = Int32.Parse(txtMinEnter.Text)
Dim hourExit As Integer = Int32.Parse(txtHourExit.Text)
Dim minuteExit As Integer = Int32.Parse(txtMinExit.Text)
Dim timeEnter As DateTime = DateTime.Today.AddHours(hourEnter).AddMinutes(minuteEnter)
Dim timeExit As DateTime = DateTime.Today.AddHours(hourExit).AddMinutes(minuteExit)
Dim parkingTime As TimeSpan = timeExit - timeEnter
lblTimeIn.Text = timeEnter.ToString("h:mm tt")
lblTimeOut.Text = timeExit.ToString("h:mm tt")
lblParkingTime.Text = parkingTime.ToString("h:mm")

vb.net arithmetic operation resulted in an overflow error

I'm trying to turn every value of an eight digit string, called "d8", into its ASCII value so then I have 8 integers. I then want to add them together to form one integer and display this value into OffFac.Text. But it always displays this error "Arithmetic operation resulted in an overflow".
Dim d8 As String
Dim Step2 As Integer
d8 = DEight.Text
Dim RandomNumber2C As Char = d8.Substring(0, 1)
Dim RandomNumber22C As Char = d8.Substring(1, 1)
Dim RandomNumber32C As Char = d8.Substring(2, 1)
Dim RandomNumber42C As Char = d8.Substring(3, 1)
Dim RandomNumber52C As Char = d8.Substring(4, 1)
Dim RandomNumber62C As Char = d8.Substring(5, 1)
Dim RandomNumber72C As Char = d8.Substring(6, 1)
Dim RandomNumber82C As Char = d8.Substring(7, 1)
Dim RandomNumberX As String = Asc(Mid(RandomNumber2C, 1))
Dim RandomNumber2X As String = Asc(Mid(RandomNumber22C,1))
Dim RandomNumber3X As String = Asc(Mid(RandomNumber32C, 1))
Dim RandomNumber4X As String = Asc(Mid(RandomNumber42C, 1))
Dim RandomNumber5X As String = Asc(Mid(RandomNumber52C, 1))
Dim RandomNumber6X As String = Asc(Mid(RandomNumber62C, 1))
Dim RandomNumber7X As String = Asc(Mid(RandomNumber72C, 1))
Dim RandomNumber8X As String = Asc(Mid(RandomNumber82C, 1))
Dim RandomNumberXX As String = CLng(RandomNumberX)
Dim RandomNumber2XX As String = CLng(RandomNumber2X)
Dim RandomNumber3XX As String = CLng(RandomNumber3X)
Dim RandomNumber4XX As String = CLng(RandomNumber4X)
Dim RandomNumber5XX As String = CLng(RandomNumber5X)
Dim RandomNumber6XX As String = CLng(RandomNumber6X)
Dim RandomNumber7XX As String = CLng(RandomNumber7X)
Dim RandomNumber8XX As String = CLng(RandomNumber8X)
Step2 = RandomNumberXX + RandomNumber2XX + RandomNumber3XX + RandomNumber4XX + RandomNumber5XX + RandomNumber6XX + RandomNumber7XX + RandomNumber8XX
Step2 = OffFac.Text
You are trying to concatenate (append a series of strings to one another) a series of numbers that are the ascii values of the numeric characters entered into your text box.
With the string "12345678" you get the following.
"49" + "50" + "51" + "52" + "53" + "54" + "55" + "56" = "4950515253545556"
The number 4950515253545556 is too large to be assigned to an integer.
Also, your last assignment statement should be
OffFac.text = step2
You probably want to do something like this
Dim RandomNumberX As Integer = Asc(Mid(RandomNumber2C, 1))
Dim RandomNumber2X As Integer = Asc(Mid(RandomNumber22C, 1))
Dim RandomNumber3X As Integer = Asc(Mid(RandomNumber32C, 1))
Dim RandomNumber4X As Integer = Asc(Mid(RandomNumber42C, 1))
Dim RandomNumber5X As Integer = Asc(Mid(RandomNumber52C, 1))
Dim RandomNumber6X As Integer = Asc(Mid(RandomNumber62C, 1))
Dim RandomNumber7X As Integer = Asc(Mid(RandomNumber72C, 1))
Dim RandomNumber8X As Integer = Asc(Mid(RandomNumber82C, 1))
Dim RandomNumberXX As Integer = CLng(RandomNumberX)
Dim RandomNumber2XX As Integer = CLng(RandomNumber2X)
Dim RandomNumber3XX As Integer = CLng(RandomNumber3X)
Dim RandomNumber4XX As Integer = CLng(RandomNumber4X)
Dim RandomNumber5XX As Integer = CLng(RandomNumber5X)
Dim RandomNumber6XX As Integer = CLng(RandomNumber6X)
Dim RandomNumber7XX As Integer = CLng(RandomNumber7X)
Dim RandomNumber8XX As Integer = CLng(RandomNumber8X)

Calculate IP in IP range

I have IP range string below:
Dim IPRange as String = "192.168.0.1-192.168.0.100"
I used code to count IP and add into Arraylist:
Dim beginIP() As Byte = IPAddress.Parse(IPRange.Split("-")(0)).GetAddressBytes
Array.Reverse(beginIP)
Dim endIP() As Byte = IPAddress.Parse(IPRange.Split("-")(1)).GetAddressBytes
Array.Reverse(endIP)
Dim IPbegin As UInt32 = BitConverter.ToUInt32(beginIP, 0)
Dim IPend As UInt32 = BitConverter.ToUInt32(endIP, 0)
Dim total as Integer = 0
Dim arr as New ArrayList()
For i As UInt32 = IPbegin To IPend
Dim IPbyte() As Byte = BitConverter.GetBytes(i)
Array.Reverse(IPbyte)
Dim IPCheck As String = New IPAddress(IPbyte).ToString
total += 1
arr.Add(IPCheck)
Next
But I have thousand IPRange like that with billion IP, loop make my application very slow. How can I speed up this code or another way to calculate IP range in this case?
Using your code
Dim stpw As Stopwatch = Stopwatch.StartNew
Dim IPRange As String = "192.168.0.0-192.200.255.255"
Dim beginIP() As Byte = IPAddress.Parse(IPRange.Split("-"c)(0)).GetAddressBytes
Array.Reverse(beginIP)
Dim endIP() As Byte = IPAddress.Parse(IPRange.Split("-"c)(1)).GetAddressBytes
Array.Reverse(endIP)
Dim IPbegin As UInt32 = BitConverter.ToUInt32(beginIP, 0)
Dim IPend As UInt32 = BitConverter.ToUInt32(endIP, 0)
Dim total As Integer = 0
For i As UInt32 = IPbegin To IPend
Dim IPbyte() As Byte = BitConverter.GetBytes(i)
Array.Reverse(IPbyte)
Dim IPCheck As String = New IPAddress(IPbyte).ToString
total += 1
Next
stpw.Stop()
Debug.WriteLine("{0:n0} in {1}", total, stpw.Elapsed)
with a larger range produced these results.
2,162,688 in 00:00:00.3756208

Countdown timer in vb.net

I'm having a start time as 11:30:25PM and an end time as 11:45:25AM
So now I would like to get the diff between the end time and the start time
This is what I have used the below code and working pretty well:
Dim dFrom As DateTime
Dim dTo As DateTime
Dim sDateFrom As String = Now.ToString("h:mm:ss tt")
Dim sDateTo As String = txtlogout1.Text
If DateTime.TryParse(sDateFrom, dFrom) AndAlso DateTime.TryParse(sDateTo, dTo) Then
Timer1.Start()
Dim TS As TimeSpan = dTo - dFrom
Dim hour As Integer = TS.Hours
Dim mins As Integer = TS.Minutes
Dim secs As Integer = TS.Seconds
Dim timeDiff As String = ((hour.ToString("00") & ":") + mins.ToString("00") & ":") + secs.ToString("00")
txtremaining1.Text = timeDiff
End If
Now the problem is if the start time is 11:30:25PM and the end time is 12:00:25AM then the remaining time is showing as -23:30:00
So what's the correct way of handling this?
Are you only concerned about spanning one day? Could you just check whether your Timespan variable is negative, and add a day if it is?
If TS.TotalMilliseconds < 0 Then TS = TS.Add(TimeSpan.FromDays(1))
If DateTime.TryParse(txtlogout1.text, dTo) Then
Dim ts as Timespan = dTo.Subtract(DateTime.now)
If ts.TotalMilliseconds < 0 Then ts = ts.Add(TimeSpan.FromDays(1))
txtremaining1.Text = ts.ToString()
End If

Find number of values before and after a character

I have the following variable.
I need to find how many numbers are before the decimal point and after the decimal point.
Dim x As Long = 123.456
I have tried converting this into a string
Dim xstr As String = x.ToString(x)
Dim searchChar As String = "."
How can I display the number of characters before the decimal point.
i.e. '3'
and also, the number of characters after the decimal point.
'3'.
You can call String.Split, like this:
Dim x As Double = 123.456
Dim xstr As String = x.ToString()
Dim searchChar As String = "."
Dim parts() As String = xstr.Split({searchChar}, StringSplitOptions.None)
Dim firstLength As Integer = parts(0).Length
Dim secondLength As Integer = parts(1).Length
Another possible solution, based on String.Substring():
Dim x As Double = 123.456
Dim xstr As String = x.ToString(NumberFormatInfo.InvariantInfo)
Dim beforeDecimalSeparator As Integer = xstr.Length
Dim afterDecimalSeparator As Integer = 0
Dim decimalSeparatorPosition As Integer = xstr.IndexOf("."c)
If decimalSeparatorPosition > -1 Then
beforeDecimalSeparator = xstr.Substring(0, decimalSeparatorPosition).Length
afterDecimalSeparator = xstr.Substring(decimalSeparatorPosition + 1).Length
End If
in your way:
xstr.Substring(xstr.IndexOf("."c) + 1)
But you do not need convert to String.
Dim y = x Mod 1
You can also use IndexOf to solve this issue.
Dim x As Double = 123.456
Dim xstr As String = x.ToString()
Dim mIndex As Integer = xstr.IndexOf(".")
Dim firstLength As Integer = mIndex;
Dim secondLength As Integer = (xstr.Length - mIndex) -1