Results 1 to 8 of 8

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

  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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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...

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    4

    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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"))
    Name:  11-07-2017 16.10.42.png
Views: 2155
Size:  3.7 KB

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

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

    Quote Originally Posted by monkey288195 View Post
    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    4

    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"
    Name:  Capture.JPG
Views: 2199
Size:  10.6 KB

    How can I make it so that the entered date if invalid it will do something?

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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.
    My usual boring signature: Nothing

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

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