I simply want to check if my strings are valid dates and then have them formatted. But "datecheck2 = IsDate(Month)" always returns as false.
edit: added comments to make code clearCode: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




Reply With Quote
