[RESOLVED] IsDate always returns false
Hi guys. Was wondering why the IsDate function always returns false.
Code:
dim myYear as string
myYear = trim(txtYear.text)
If isYear(myYear) = false then
msgbox("Not a year")
End if
Do I have to define the myYear variable of the date data type? Or is there some other problem?
Re: IsDate always returns false
If you want to determine whether the contents of a string is a valid date use Date.TryParse, ie
Code:
Dim x As String = "12/02/2010"
Dim dateresult As Date
If Date.TryParse(x, dateresult) Then
MessageBox.Show("Yes its a date")
Else
MessageBox.Show("No its not a date")
End If
Re: IsDate always returns false
It doesn't seem to work for values that contain only a year.
Re: IsDate always returns false
Well a year isn't actually a date is it? Its just an integer.
What form of validation are you looking to do? Would you consider 35012 to be a year?
Re: IsDate always returns false
I wanted to make sure that the user inputs a valid date value, which I can also use to determine if the book is already outdated (eg: to check if the book was published 10 years or earlier from now). It's okay if I use an integer for it, but when I try to make a report using CR, it shows the Year Published value with a comma (eg: 2,010).
Re: IsDate always returns false
Quote:
It's okay if I use an integer for it, but when I try to make a report using CR, it shows the Year Published value with a comma (eg: 2,010).
In that case you need to change the formatting within CR. Unfortunately I can't really help you with that but it can't be that hard.
Re: IsDate always returns false
There is a control that might help. It is DateTimePicker.
Code:
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DateTimePicker1.ValueChanged
'check selected date against past date
If DateTime.Now.Date.AddYears(-10) > DateTimePicker1.Value.Date Then
Stop
End If
End Sub
Re: IsDate always returns false
Quote:
Originally Posted by
dbasnett
There is a control that might help. It is DateTimePicker.
Code:
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DateTimePicker1.ValueChanged
'check selected date against past date
If DateTime.Now.Date.AddYears(-10) > DateTimePicker1.Value.Date Then
Stop
End If
End Sub
Yeah but the OP doesn't want a date as such - just a year. the DTP picks an actual date.
Re: IsDate always returns false
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.CustomFormat = "yyyy"
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.ShowUpDown = True
End Sub
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DateTimePicker1.ValueChanged
'check selected date against past date
If DateTime.Now.Date.AddYears(-10) > DateTimePicker1.Value.Date Then
Stop
End If
End Sub