vb.net date of the day after every 24 hours - vb.net

I have a timer which is to tick after every 86400000seconds (every 24 hours and therefore every day) to put the new day in a DateTimePicker, dtPicker1. Unfortunately, after the timer ticks the result brings the time of the day in addition to the date (for example 2017-03-18 00:00:05, instead of 2017-03-18. When I use the messagebox to check the output, I get the date without the time but the next day (after the timer ticks, it adds the time and therefore the code fails.
`'ticks every 86400000 miliseconds (a day)
dtPicker1.Enabled = True
dtPicker1.Text = ""
Dim mFmt As String = "yyyy-MM-dd"
dtPicker1.Format = DateTimePickerFormat.Custom
dtPicker1.CustomFormat = mFmt
dtPicker1.Text = ""
Dim st As Date = Today.AddDays(-1).ToShortDateString
Dim cf As String = Today.AddDays(-1)
Dim dr As String = cf.Substring(3, 2)
'MsgBox(dr)
Dim de As String = cf.Substring(6, 4)
Dim fg As String = cf.Substring(3, 2)
Dim ab As String = cf.Substring(0, 2)
'MsgBox(de & "-" & fg & "-" & ab, vbInformation, "Here you Go")
dtPicker1.Value = de & "-" & fg & "-" & ab`
Help will be greatly appreciated.

You don't need to use substrings, you can set the value directly as a date.
dtPicker1.Text = ""
Dim mFmt As String = "yyyy-MM-dd"
dtPicker1.Format = DateTimePickerFormat.Custom
dtPicker1.CustomFormat = mFmt
dtPicker1.Value = Today.AddDays(-1)

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")

How to get and subtract the minute in datetimepicker to show on the textbox? (im using vb.net)

When i'm trying to compute time in my datetimepicker for example 12:00 AM - 3:40 AM the result is showing 3:-40.
Please help!
Dim HoursDiff = DateDiff(DateInterval.Hour, DTPTS.Value, DTPTE.Value)
Dim minutesDiff = DateDiff(DateInterval.Minute, DTPTS.Value, DTPTE.Value)
Dim totalMin As Integer = minutesDiff Mod 60
Dim Total As String = HoursDiff & ":" & totalMin
total.Text = Total
Dim totalmin2 As Integer = totalMin2 - txtboxMM.Text
Dim totalhours As Integer = HoursDiff - TxtboxHH.Text
Dim total2 As String = totalhours & ":" & totalmin2
TxtboxTRTint.Text = total2`
The easiest and most convenient way to subtract total minutes is by using TimeSpan.FromMinutes() which picks TotalMinutes property from DateTime.Subtract() like this:
Dim TotalMin = TimeSpan.FromMinutes((DTPTE.Value.Subtract(DTPTS.Value)).TotalMinutes)
TxtboxTRTint.Text = String.Format("{0}:{1:00}", TotalMin.Hours, TotalMin.Minutes)
The result now should be 3:40.
Example demo: .NET Fiddle

Best Way to check if a returned string value from a database is in the format "hh:mm:ss"

I'm getting back ":" (no spaces), " : " (spaces on both sides), " :" (leading space) or ": " (trailing space) from some of the data im pulling from sql. I want to display date to the user only if its in the correct format "01:21:01". This will get put to a date picker set for time format. I have some code below, but it isn't working for me:
Dim D As Date
IF ds.Tables(0).Rows(1)("Pond1SampleTime").ToString() = "" Or Date.TryParseExact(ds.Tables(0).Rows(1)("Pond1SampleTime").ToString(), "hh:mm", Nothing, Globalization.DateTimeStyles.NoCurrentDateDefault, D) Then
Ponds1SampleTimePicker.Value = "1/1/1753 12:00:00 AM"
Else
Ponds1SampleTimePicker.Value = SelDat + " " + ds.Tables(0).Rows(1)("Pond1SampleTime").ToString()
End If
Any ideas what im doing wrong?
You can use TimeSpan.TryParse if you only want to parse the hour, minutes and seconds of the string.
However, looking at your code you it looks like you are trying to parse a full date and set a date picker value. In this case append use DateTime.TryParse to try to parse the selData + [your string]
Dim ts As TimeSpan
Dim d As Date
Dim hhmmss as string =ds.Tables(0).Rows(1)("Pond1SampleTime").ToString()
Dim ds as string = SelDat & " " & hhmmss
' Parse with TimeSpan
If TimeSpan.TryParse(hhmmss, ts) Then
Ponds1SampleTimePicker.Value = ds
Else
Ponds1SampleTimePicker.Value = "1/1/1753 12:00:00 AM"
End if
' Parse with DateTime
If DateTime.TryParse(ds, d) Then
Ponds1SampleTimePicker.Value = ds
End if

Trying to start Stopwatch based off of old split saved in .txt file

The closest I have gotten is to get the last line in the .txt file (there are a bunch of splits saved), and then add that to the current elapsed time, and then display THAT in the label box:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim lines() As String = IO.File.ReadAllLines(filepath)
Dim lastline As String
lastline = lines(lines.Length - 1)
Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
Dim pastHours As String
pastHours = lastline.Substring(0, 2)
Dim currentHours As Double = elapsed.Hours + pastHours
Dim pastMinutes As String
pastMinutes = lastline.Substring(3, 2)
Dim currentMinutes As Double = elapsed.Minutes + pastMinutes
Dim pastSeconds As String
pastSeconds = lastline.Substring(6, 2)
Dim currentSeconds As Double = elapsed.Seconds + pastSeconds
Dim pastMilliseconds As String
pastMilliseconds = lastline.Substring(9, 3)
Dim currentMilliseconds As Double = elapsed.Milliseconds + pastMilliseconds
Label1.Text = Format(currentHours, "00") & ":" & Format(currentMinutes, "00") & ":" & Format(currentSeconds, "00") & "." & (currentMilliseconds)
End Sub
The only problem with that, is that instead of having 1000 milliseconds in a second, 60 seconds in a minute, etc, it will go up to 60 PLUS whatever that last split has saved. For example, if the last split saved was 34 seconds, it will take until a displayed amount of 94 seconds before it ticks up a minute, and even then it will start the seconds over at 34 seconds.
You can add TimeSpan values to each other. In your case, you could do:
Dim pastElapsed As TimeSpan = New TimeSpan(0, pastHours, pastMinutes, pastSeconds, pastMilliseconds)
Dim currentElapsed As TimeSpan = elapsed + pastElapsed
Dim formatted As String = Format(currentElapsed.Hours, "00") & ":" & Format(currentElapsed.Minutes, "00") & ":" & Format(currentElapsed.Seconds, "00") & "." & Format(currentElapsed.Milliseconds, "000")
Label1.Text = formatted

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