[RESOLVED] DateTime format validation method using DateTime.TryParse() always returns false??
So, simply put I made a quick method that returns true if a format provided is a valid for a date time to string representation..
This all seems like it should work to me, and even with some googling I've had no luck.
The method is..
Code:
Private Function ValidateDateTimeFormat(ByVal format As String) As Boolean
Dim dt As DateTime = DateTime.MinValue
Dim result As Boolean = False
If DateTime.TryParse(format, dt) Then 'Also tried...and TryParseExact as some forum solutions suggested.
'If DateTime.TryParse(format, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
result = True
End If
dt = Nothing
Return result
End Function
Results(for me) are...
yyyy = False
dddd, MMMM dd, yyyy = False
etc etc..
In all honestly I manually set my condition to true and everything displays fine, just as it should. Only when I replace that True with my method does it always returns false, no matter the format provided.
Thanks in advance
David Carrigan
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Firstly, do you want to enforce one or more specific formats or do you just want to validate against what's valid on the local machine?
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Right.... because dddd, MMMM dd, yyyy isn't a valid date.... it's a valid FORMAT for a date... but the tryParse takes two parameters... the first is a STRING varaible which contains a date to convert.... the second parameter is where the converted date should go if successfully converted.
Based on the name of the function and what you're passing in, it sounds like your're trying to validate the format of a date... TryParse parses a date string... not a format string...
What you probably should do is convert the date to a string using the passed in format, then try to TryParse that result...
Code:
Private Function ValidateDateTimeFormat(ByVal format As String) As Boolean
Dim s As String= DateTime.MinValue.ToString(format) 'Create a Date String using the format passed into the function
Dim dt As DateTime
Dim result As Boolean = False
If DateTime.TryParse(s, dt) Then ' Here s is the formatted date, that is what you should be passing in.
result = True
End If
dt = Nothing
Return result
End Function
-tg
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Quote:
Originally Posted by
jmcilhinney
Firstly, do you want to enforce one or more specific formats or do you just want to validate against what's valid on the local machine?
I'm actually using this in the set for a property of a control inheriting the Label control. The property is Format, similar to the DateTimePicker controls. I'm only validating whatever the user chooses to pass. If it's not valid then the format doesn't change, error message informs the user the format is invalid etc etc.
But thank you techgnome, that was it! I honestly cannot believe I made that mistake! You are absolutely right, and my approach never would have worked. kudos, and thanks again.
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Techgnome, I would give you some rep. But apparently you help me to often :rolleyes: I guess I'll have to wait someone else to help me before I can give you whats rightfully yours.
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Quote:
Originally Posted by
techgnome
Right.... because dddd, MMMM dd, yyyy isn't a valid date.... it's a valid FORMAT for a date... but the tryParse takes two parameters... the first is a STRING varaible which contains a date to convert.... the second parameter is where the converted date should go if successfully converted.
Based on the name of the function and what you're passing in, it sounds like your're trying to validate the format of a date... TryParse parses a date string... not a format string...
What you probably should do is convert the date to a string using the passed in format, then try to TryParse that result...
Code:
Private Function ValidateDateTimeFormat(ByVal format As String) As Boolean
Dim s As String= DateTime.MinValue.ToString(format) 'Create a Date String using the format passed into the function
Dim dt As DateTime
Dim result As Boolean = False
If DateTime.TryParse(s, dt) Then ' Here s is the formatted date, that is what you should be passing in.
result = True
End If
dt = Nothing
Return result
End Function
-tg
If you're not going to do anything with the result of a .TryParse in VB.NET you can set the result to Nothing rather than creating a temporary variable you need to get rid of.
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim now = DateTime.Now()
Dim format1 = "MM/dd/yyyy"
Dim format2 = "ab/cd/efgh"
Dim isFormat1Valid = ValidateDateTimeFormat(now, format1)
Dim isFormat2Valid = ValidateDateTimeFormat(now, format2)
End Sub
Private Function ValidateDateTimeFormat(dt As DateTime, format As String) As Boolean
Return DateTime.TryParse(dt.ToString(format), Nothing)
End Function
Re: DateTime format validation method using DateTime.TryParse() always returns false?
I assumed that you meant that you were passing date strings in those formats, not the format strings themselves.
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Quote:
If you're not going to do anything with the result of a .TryParse in VB.NET you can set the result to Nothing rather than creating a temporary variable you need to get rid of.
Good call on that. Actually up until now I've never considered having Nothing as the return on a .TryParse(). Thanks!
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Quote:
Originally Posted by
jmcilhinney
I assumed that you meant that you were passing date strings in those formats, not the format strings themselves.
Yup, precisely. In all honesty it's been a rough two weeks for me, now I'm getting sick. My heads' just not in the right place :rolleyes:, thanks again.
Re: DateTime format validation method using DateTime.TryParse() always returns false?
Quote:
Originally Posted by
DavesChillaxin
Good call on that. Actually up until now I've never considered having Nothing as the return on a .TryParse(). Thanks!
It's not that you're using Nothing as the return. The return value is a Boolean regardless. The output parameter is a DateTime. If you pass Nothing to that parameter then, inside the TryParse method, it will receive the default value for the DateTime type, which is #1/01/0001#. If the conversion works then the method will assign the appropriate DateTime value to the parameter and that is what the method outputs. The only difference is that, because you used a literal rather than a variable, you don't have access to that output value.