I am trying to convert string into date on vb, but for some reason I get the following error message.
System.FormatException: 'String was not recognized as a valid DateTime.'
I am getting the date from date control on winform
dim selectedDate as string = dateControl1.text
dim selectedDt as Date = Date.Parse(selectedDate)
but if I put the same value in code instead of getting from dateControl, is ok
dim selectedDate as string = "19/01/2021"
dim selectedDt as Date = Date.Parse(selectedDate)
so I try to see if there is any different between value from dateControl1 and when type in the code since dateControl1.text should returning the same string value so I try to do this
dim selectedDate as string = "19/01/2021"
if selectedDate = dateControl1.text then
debug.print ("same")
else
debug.print ("different")
end if
when I run it, debug print "same"
and if I do this
dim selectedDate as string = "19/01/2021"
if selectedDate = dateControl1.text then
debug.print ("same")
else
debug.print ("different")
end if
dim selectedDt as Date = Date.Parse(dateControl1.text)
I get the same error even though the debug say "same"
so I am really confuse, why date.parse show error when I am getting the value from dateControl1.text and it's exactly the same string
am I missing something?
Thank you for your help.
Regards
- Haris -
DateTime.TryParse(), providing the parsing format. What is a date control (a control provided by what platform)? Doesn't it provide aValueproperty that returns a Date object? - JimiParseExactorTryParseExactand specify the exact format or else callParseorTryParseand provide an IFormatProvider that provided the appropriate date format. As suggested though, if you're using WinForms and aDateTimePickercontrol then you should not be touching itsTextproperty but, rather, using theValueproperty. - jmcilhinney