Results 1 to 8 of 8

Thread: How to I check if a date is valid then reformat it?

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    4

    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
    Last edited by monkey288195; Jul 11th, 2017 at 09:47 AM. Reason: Added comments

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width