How to I check if a date is valid then reformat it?
I simply want to check if my strings are valid dates and then have them formatted. But "datecheck2 = IsDate(Month)" always returns as false.
Code:
Dim datecheck As Boolean
Dim yearMonth As String = yearandmonth ' currently looks like yyyy-MM
Dim allowedFormat() As String = {"yyyy-MM"}
Dim dd As DateTime
datecheck = IsDate(yearMonth) ' returns true or false if date is valid. EX: 2017-13 = false because "13" is over 12
If datecheck = True Then ' if the date is valid I want it to be reformatted to "yyyy-MM"
DateTime.TryParseExact(yearMonth, allowedFormat, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dd)
Dim yearMonthFormat As String = dd.ToString("yyyy-MM")
Else
MsgBox("Invalid year/month")
End If
Dim datecheck2 As Boolean
Dim Month As String = filemonth ' string looks like this: "MM"
Dim allowedFormat2() As String = {"MM - MMMM"}
Dim dt As DateTime
datecheck2 = IsDate(Month) ' THIS RETURNS FALSE FOR MONTH. Example: If Month = "05" then it returns false. Even though it is a valid month
MsgBox(datecheck2)
If datecheck2 = True Then
'this reformats it to "MM - MMMM"
DateTime.TryParseExact(Month, allowedFormat2, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt)
Dim monthFormat As String = dt.ToString("MM - MMMM")
MsgBox(monthFormat)
Else
MsgBox("Invalid Month") ' change to log
End If
edit: added comments to make code clear
Re: How to I check if a date is valid then reformat it?
A year and month string is never a valid date. It must have a day too. You should use tryparseexact to check it...
Re: How to I check if a date is valid then reformat it?
Doesn't tryparseeexact simply reformat the string? Sorry I'm a bit confused
1 Attachment(s)
Re: How to I check if a date is valid then reformat it?
this is how it works:
Code:
Dim d As Date
Date.TryParseExact("03052011", "ddMMyyyy", Nothing, Nothing, d)
MsgBox(d.ToString("dddd d MMMM yyyy"))
Attachment 149463
Re: How to I check if a date is valid then reformat it?
Quote:
Originally Posted by
monkey288195
Doesn't tryparseeexact simply reformat the string? Sorry I'm a bit confused
No, it tries to convert the String to a DateTime, but only if the String matches the exact format specified.
To convert the DateTime to a String in a particular format, use .ToString with the appropriate format (as .paul. showed).
Note that as TryParseExact returns a Boolean, you can use that in the If statement instead of datecheck = True
1 Attachment(s)
Re: How to I check if a date is valid then reformat it?
Ok that makes sense.
But it returns a value regardless of invalid date
Code:
Dim dd As Date
Date.TryParseExact("32", "MM", Nothing, Nothing, dd) ' if "32" is entered as a month it still returns a value.
MsgBox(dd.ToString("MM - MMMM")) ' returns "01 - January"
Attachment 149467
How can I make it so that the entered date if invalid it will do something?
Re: How to I check if a date is valid then reformat it?
You MUST check the return from TryParseExact for this to work. You aren't really seeing a value returned, you are seeing an artifact of the date being a value type. Since it's a value type, it can't have no value. Before you even call TryParseExact, dd has a value, which is a valid date of 1/1/1900 (or something like that, there are other default values, and I don't quite remember the default value used in .NET). In this case, TryParseExact is probably returning False, because it is not a valid date. If TryParseExact returns false, then no conversion was done and dd holds the same default value that it had to start with.
So, you can only use dd if TryParseExact returned True. Otherwise, the value in dd will be the default value for a date.
Re: How to I check if a date is valid then reformat it?
Code:
Dim d As Date
If Date.TryParseExact("32", "MM", Nothing, Nothing, d) Then
MsgBox(d.ToString("dddd d MMMM yyyy"))
Else
MsgBox("32 is not a month")
End If