CDate for mm/dd/yyy hh:mm:ss
Need to write a CDate that is correct.
CDate(mm/dd/yyyy hh:mm:ss)
Code:
alarm = CDate(TextBox10.Text & "/" & TextBox7.Text & "/" & TextBox23.Text & " " & TextBox2.Text & ":" & TextBox3.Text & ":" & TextBox14.Text)
aampm = ComboBox1.Text
ahour = alarm.Hour
amin = alarm.Minute
asec = alarm.Second
amo = alarm.Month
ady = alarm.Day
ayr = alarm.Year
Re: CDate for mm/dd/yyy hh:mm:ss
Don't use CDate. While it can be used to convert a String to a Date, it shouldn't be. It should only be used to cast an Object reference that is already a Date, e.g. from a DataRow:
vb.net Code:
Dim dob = CDate(row("DateOfBirth"))
In your case, why not just use a DateTimePicker control instead of all those separate TextBoxes? That will do the validation and you can simply get a Date from the Value property. If you're determined to use TextBoxes then you should be validating the input first and then using the Date constructor, e.g.
vb.net Code:
Dim year As Integer
'...
Dim seconds As Integer
Dim alarmTime As Date
If Integer.TryParse(TextBox10.Text, year) AndAlso
'...
Integer.TryParse(TextBox14.Text, seconds) AndAlso seconds >= 0 AndAlso seconds < 60 Then
alarmTime = New Date(year, ..., seconds)
'...
End If
If you were going to use a String then you should be using Date.TryParse or Date.TryParseExact:
vb.net Code:
Dim alarmTime As Date
If Date.TryParse($"{TextBox10.Text}/{TextBox7.Text}/{TextBox23.Text} {TextBox2.Text}:{TextBox3.Text}:{TextBox14.Text}", alarmTime) Then
'...
End If
Also, what's up with those control names? The numbers are all over the place so there's a great chance that you could use the wrong on in the wrong place but you should not be using numbers anyway. DO NOT just accept the default names for controls. ALWAYS provide your own meaningful name, e.g. yearTextBox and secondsTextBox. That makes your code far easier to read and less error-prone.
Re: CDate for mm/dd/yyy hh:mm:ss
Try this. Note the format...
Code:
"MM/dd/yyyy hh:mm:ss"
That's 12 hour clock with no AM/PM. To add that...
Code:
"MM/dd/yyyy hh:mm:ss tt"
To use 24 hour clock instead...
Code:
"MM/dd/yyyy HH:mm:ss"
This is for parsing the date from the string...
Code:
alarm = Date.ParseExact(TextBox10.Text & "/" & TextBox7.Text & "/" & TextBox23.Text & " " & TextBox2.Text & ":" & TextBox3.Text & ":" & TextBox14.Text, "MM/dd/yyyy hh:mm:ss", Nothing)
Re: CDate for mm/dd/yyy hh:mm:ss
If you have all of the parts in individual text boxes, why build a string and then parse it? The Date data type has an overload that will take the values and avoid having to build a string.
https://docs.microsoft.com/en-us/dot...datetime.-ctor