Results 1 to 24 of 24

Thread: Date Must Be Between 1900 and 2100

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Date Must Be Between 1900 and 2100

    Okay so basically my program is a program that validates dates in this format:
    DD/MM/YYYY

    However, I need I guess some sort of statement (my brain is thinking an IF ELSE) to make sure that the user does not enter a date that is less than 1900 and not greater than 2100; or in other words: it must be between 1900 and 2100.

    I don't suppose anyone could help me? The code I have done so far is here:
    Code:
        Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
            Dim dateString, format As String
            Dim result As Date
            Dim IsValid As Boolean = False
            dateString = txtDate.Text
            format = "dd/M/yyyy"
    
            If format <  Then
                IsValid = False
            End If
    
            Try
                result = Date.ParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture)
                IsValid = True
            Catch
                IsValid = False
            End Try
                If (IsValid) Then
                    MessageBox.Show("Valid.")
                Else
                    MessageBox.Show("Invalid.")
                End If
        End Sub
    The validation works but the assurance of the year doesn't work.

    Thanks.

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Date Must Be Between 1900 and 2100

    Start by proofreading:

    format = "dd/M/yyyy"

    If format < Then


    Only one digit for Month? And if format is less than what? Also, format is a string, how can it be less than anything?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Date Must Be Between 1900 and 2100

    Use a DateTimePicker! You can set the min and max dates and it doesn't need any validation, making your entire code here redundant!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    It's a school thing that I have to do. I can't use a DateTimePicker.

    @circuits2: Okay so how can I do it?

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Date Must Be Between 1900 and 2100

    Some ideas

    Code:
            Dim dtMin As DateTime = #1/1/1900#
            Dim dtMax As DateTime = #12/31/2100#
    
            Dim myDate As DateTime = DateTime.Now
    
            If myDate >= dtMin AndAlso myDate <= dtMax Then
                'valid date
            Else
                'invalid date
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    I think you're on the right tracks, but basically I need to check the date that was entered in by the user.

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Date Must Be Between 1900 and 2100

    It's a school thing that I have to do.
    Ah, right. Should have led with that. Then we'd know to expect you to have to do everything the most difficult and inefficient way possible.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    Heh, yes, there are better ways. .

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Date Must Be Between 1900 and 2100

    Quote Originally Posted by D.E.L.B. View Post
    I think you're on the right tracks, but basically I need to check the date that was entered in by the user.
    See http://msdn.microsoft.com/en-us/libr....tryparse.aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Date Must Be Between 1900 and 2100

    you know that once you coerce the value into a date variable... it has a .Year property... and you can see if
    myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100


    ...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    http://puu.sh/2Km3U.jpg

    This is what my window looks like. So basically the user enters the text into the textbox (it's named txtDate) and then I press the button and it validates the format of the date and if it's between 1900 and 2100.

    Code:
            If result.Year >= dtMin AndAlso result.Year <= dtMax Then
                IsValid = True
            Else
                IsValid = False
            End If
    I tried this here but it doesn't work either.

    @circuits2: I put just the one M because this type of date is valid:
    13/05/1990

    As well as this:
    13/5/1990

    @dbasnett: If you could explain to me how I can implement that in my code it would be greatly appreciated.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Date Must Be Between 1900 and 2100

    Show us what you have tried using TryParse or TryParseExact
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Date Must Be Between 1900 and 2100

    Code:
    If result.Year >= dtMin AndAlso result.Year <= dtMax Then
    That's not what I said was it?

    what I said was this:
    Code:
    myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100
    are you trying to compare the YEARS or the FULL DATE? your first post indicated that after checking to see if it's a valid date, then you need to check that the date is between 1900 and 2100 ... which are years... not dates...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Re: Date Must Be Between 1900 and 2100

    Quote Originally Posted by techgnome View Post
    Code:
    If result.Year >= dtMin AndAlso result.Year <= dtMax Then
    That's not what I said was it?

    what I said was this:
    Code:
    myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100
    are you trying to compare the YEARS or the FULL DATE? your first post indicated that after checking to see if it's a valid date, then you need to check that the date is between 1900 and 2100 ... which are years... not dates...

    -tg
    He could be labeling dtMin and dtMax as the year values perhaps? He wouldn't be able to compile it otherwise.

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Date Must Be Between 1900 and 2100

    possibly, but then he stated this "I tried this here but it doesn't work either." ... which doesn't really tell me much either... for all I know it means his fingers fell off...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Date Must Be Between 1900 and 2100

    Quote Originally Posted by D.E.L.B. View Post
    http://puu.sh/2Km3U.jpg

    This is what my window looks like. So basically the user enters the text into the textbox (it's named txtDate) and then I press the button and it validates the format of the date and if it's between 1900 and 2100.

    Code:
            If result.Year >= dtMin AndAlso result.Year <= dtMax Then
                IsValid = True
            Else
                IsValid = False
            End If
    I tried this here but it doesn't work either.

    @circuits2: I put just the one M because this type of date is valid:
    13/05/1990

    As well as this:
    13/5/1990

    @dbasnett: If you could explain to me how I can implement that in my code it would be greatly appreciated.
    Assuming result is a datetime and dtMin and dtMax are what I posted previously:

    Code:
            If result >= dtMin AndAlso result <= dtMax Then
                IsValid = True
            Else
                IsValid = False
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    Quote Originally Posted by techgnome View Post
    Code:
    If result.Year >= dtMin AndAlso result.Year <= dtMax Then
    That's not what I said was it?

    what I said was this:
    Code:
    myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100
    are you trying to compare the YEARS or the FULL DATE? your first post indicated that after checking to see if it's a valid date, then you need to check that the date is between 1900 and 2100 ... which are years... not dates...

    -tg
    I am trying to compare the YEARS, sorry.

  18. #18

    Re: Date Must Be Between 1900 and 2100

    Quote Originally Posted by D.E.L.B. View Post
    I am trying to compare the YEARS, sorry.
    Then...what's the problem? There are two (maybe three, I haven't counted the entire thread) different posts that show you exactly what to do.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    I'm not messing you guys about but I'm overwhelmed with the amount of solutions to this problem. I haven't use DateTime.TryParse and I have no idea how to implement it. I have looked at MS' documentation but it doesn't make sense to me.

  20. #20
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Date Must Be Between 1900 and 2100

    Step 1. try to convert the string into a date using DateTime.TryParse
    Code:
    Dim DateValue as Date
    If Date.TryParse (TextBoxUserInputtedDate.Text , DateValue) Then
       'Good date
    Else
       MessageBox.Show ("Invalid date entered")
    End If
    Step 2. Now you have it as a date variable, check it is within the correct year range as per #10 Techgnome

    Code:
    Dim DateValue as Date
    If Date.TryParse (TextBoxUserInputtedDate,DateValue) Then
        If DateValue.Year >= 1900 andalso DateValue.Year <= 2100 Then
           'This is within the range  so accept the input as correct
        Else
          MessageBox.Show ("Valid date entered but it is outside the required range")
        End If
    Else
       MessageBox.Show ("Invalid date entered")
    End If

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    Thanks!

  22. #22
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Date Must Be Between 1900 and 2100

    I should have explained this in the last post too since you said you didn't understand the MSDN documentation. I hope my explanation makes it clearer.
    Code:
    If Date.TryParse (TextBoxUserInputtedDate.Text , DateValue) Then
    TryParse tries to convert from one datatype to another. In the above example, you want to convert the string TextBoxUserInputtedDate.Text to a date.
    If it is a valid date it sets the second variable in the argument list (DateValue) to that date.
    If it is not a valid date, it returns False, therefore allowing us to do
    Code:
    If Date.TryParse (...) = True  Then

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    19

    Re: Date Must Be Between 1900 and 2100

    Thank you, explained very clearly!

    I just noticed something however, my DateTime has to be in the format of DD/MM/YYYY , the year formatting is correct though. I don't suppose you or anyone else could help me, please?

  24. #24

    Re: Date Must Be Between 1900 and 2100

    You should really try and understand MSDN's documentation. However, I'll give you a hand with these two functions.

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