Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. CtypeDynamic : How can i detect if an expression can be converted to a specific Date format

CtypeDynamic : How can i detect if an expression can be converted to a specific Date format

Scheduled Pinned Locked Moved Visual Basic
questioncsharp
13 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D desanti

    Hello ! I'm using vb.net 2017. I have a code where I should use CTypeDynamic. With other types everything is ok , but if the target type is System.DateTime , I want to detect if the expression can be converted to a Date with the format "dd/mm/yyyy. How can I do that with CTypeDynamic? Thank you !

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Convert it to a string, tryparsing it as a date?

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

    D 1 Reply Last reply
    0
    • L Lost User

      Convert it to a string, tryparsing it as a date?

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      D Offline
      D Offline
      desanti
      wrote on last edited by
      #3

      I have it as a string , But if I use CTypeDynamic(str, type1) , and str="11/26/2018" , it shows as correct , but it shouldn't be because is not correct with the format dd/MM/yyyy

      L 1 Reply Last reply
      0
      • D desanti

        I have it as a string , But if I use CTypeDynamic(str, type1) , and str="11/26/2018" , it shows as correct , but it shouldn't be because is not correct with the format dd/MM/yyyy

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        The correct date-format is determined by the system settings, and thus, by the user. You choose a date where it is easy to see which is the day-part and which is the month-part. Now, how about 3/4/2001? The computer isn't allowed to guess, and you can't guess the "correct" place of day's and month within that date.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        D 1 Reply Last reply
        0
        • L Lost User

          The correct date-format is determined by the system settings, and thus, by the user. You choose a date where it is easy to see which is the day-part and which is the month-part. Now, how about 3/4/2001? The computer isn't allowed to guess, and you can't guess the "correct" place of day's and month within that date.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          D Offline
          D Offline
          desanti
          wrote on last edited by
          #5

          I want that my program works regardless of how user configure the system date format. So I choose the format dd/MM/yyyy , and the user should input the date in this format. 3/4/2001 should be interpreted as April 3 2001. 26/11/2018 is November 26 11/26/2018 - is invalid

          L 1 Reply Last reply
          0
          • D desanti

            I want that my program works regardless of how user configure the system date format. So I choose the format dd/MM/yyyy , and the user should input the date in this format. 3/4/2001 should be interpreted as April 3 2001. 26/11/2018 is November 26 11/26/2018 - is invalid

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            desanti wrote:

            3/4/2001 should be interpreted as April 3 2001. 26/11/2018 is November 26 11/26/2018 - is invalid

            So, any date that can be interpreted as M/D/Y should be. Anything that fails is to be assumed D/M/Y, and if that fails, it is invalid. No, I will not participate. Your data is all in the same format, or the entire dataset must be considered invalid. There's a good reason why DateTime.TryParse Method (System) | Microsoft Docs[^] only accepts a single provider. If you don't know what the format is, and start guessing like you do, you're bound to end up with multiple small disasters.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

            D 1 Reply Last reply
            0
            • L Lost User

              desanti wrote:

              3/4/2001 should be interpreted as April 3 2001. 26/11/2018 is November 26 11/26/2018 - is invalid

              So, any date that can be interpreted as M/D/Y should be. Anything that fails is to be assumed D/M/Y, and if that fails, it is invalid. No, I will not participate. Your data is all in the same format, or the entire dataset must be considered invalid. There's a good reason why DateTime.TryParse Method (System) | Microsoft Docs[^] only accepts a single provider. If you don't know what the format is, and start guessing like you do, you're bound to end up with multiple small disasters.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              D Offline
              D Offline
              desanti
              wrote on last edited by
              #7

              Maybe we are misunderstanding each other. The situation is simple : The user write a date in a textbox. I want that the user write the date in the format : day/month/year. So I want a way to check if what the user has wrote can be converted to a date in that format : - 01/05/2015 - is ok because 01 is a valid for day number , 05 is ok because is valid for month number , 2015 is ok because is valid for year number. - 25/12/2018 - is ok because 25 is a valid for day number , 12 is ok because is valid for month number , 2018 is ok because is valid for year number. - 12/25/2018 - is wrong because 12 is a valid for day number , 25 is wrong because is not valid for month number , 2018 is ok because is valid for year number. So it's wrong !!! What is the problem with this logic ?????

              L 2 Replies Last reply
              0
              • D desanti

                Maybe we are misunderstanding each other. The situation is simple : The user write a date in a textbox. I want that the user write the date in the format : day/month/year. So I want a way to check if what the user has wrote can be converted to a date in that format : - 01/05/2015 - is ok because 01 is a valid for day number , 05 is ok because is valid for month number , 2015 is ok because is valid for year number. - 25/12/2018 - is ok because 25 is a valid for day number , 12 is ok because is valid for month number , 2018 is ok because is valid for year number. - 12/25/2018 - is wrong because 12 is a valid for day number , 25 is wrong because is not valid for month number , 2018 is ok because is valid for year number. So it's wrong !!! What is the problem with this logic ?????

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                Why make things difficult for yourself and your user? Use a Calendar Class (System.Windows.Controls) | Microsoft Docs[^]

                D 1 Reply Last reply
                0
                • L Lost User

                  Why make things difficult for yourself and your user? Use a Calendar Class (System.Windows.Controls) | Microsoft Docs[^]

                  D Offline
                  D Offline
                  desanti
                  wrote on last edited by
                  #9

                  Ok , what about the case when i'm reading the strings from a text File ?

                  L 1 Reply Last reply
                  0
                  • D desanti

                    Maybe we are misunderstanding each other. The situation is simple : The user write a date in a textbox. I want that the user write the date in the format : day/month/year. So I want a way to check if what the user has wrote can be converted to a date in that format : - 01/05/2015 - is ok because 01 is a valid for day number , 05 is ok because is valid for month number , 2015 is ok because is valid for year number. - 25/12/2018 - is ok because 25 is a valid for day number , 12 is ok because is valid for month number , 2018 is ok because is valid for year number. - 12/25/2018 - is wrong because 12 is a valid for day number , 25 is wrong because is not valid for month number , 2018 is ok because is valid for year number. So it's wrong !!! What is the problem with this logic ?????

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10
                    Dim source As String() = {"01/05/2015", "25/12/2018", "12/25/2018"}
                    
                    Sub Main()
                    
                        For Each dateCandidate As String In source
                            Dim dateResult As Date
                            If Date.TryParseExact(
                                dateCandidate, "d/M/yyyy",
                                CultureInfo.InvariantCulture,
                                DateTimeStyles.None,
                                dateResult) Then
                                Console.WriteLine("Found {0} as valid.", dateResult)
                            Else
                                Console.WriteLine("Found {0} to be invalid.", dateCandidate)
                            End If
                        Next
                        Console.ReadKey()
                    End Sub
                    

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                    D 1 Reply Last reply
                    0
                    • D desanti

                      Ok , what about the case when i'm reading the strings from a text File ?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      Then you do what Eddy already suggested and use DateTime.TryParse Method (System) | Microsoft Docs[^]. Try and make more use of Google and the MSDN documentation.

                      1 Reply Last reply
                      0
                      • L Lost User
                        Dim source As String() = {"01/05/2015", "25/12/2018", "12/25/2018"}
                        
                        Sub Main()
                        
                            For Each dateCandidate As String In source
                                Dim dateResult As Date
                                If Date.TryParseExact(
                                    dateCandidate, "d/M/yyyy",
                                    CultureInfo.InvariantCulture,
                                    DateTimeStyles.None,
                                    dateResult) Then
                                    Console.WriteLine("Found {0} as valid.", dateResult)
                                Else
                                    Console.WriteLine("Found {0} to be invalid.", dateCandidate)
                                End If
                            Next
                            Console.ReadKey()
                        End Sub
                        

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                        D Offline
                        D Offline
                        desanti
                        wrote on last edited by
                        #12

                        Thank you , Your code works with these examples and many others. but as I've said , I read values from a text file . Sometimes the string that come from text file is like this : 25/12/2018 00:01:00 This is correct because the date part is correct , but for your code this is invalid.

                        L 1 Reply Last reply
                        0
                        • D desanti

                          Thank you , Your code works with these examples and many others. but as I've said , I read values from a text file . Sometimes the string that come from text file is like this : 25/12/2018 00:01:00 This is correct because the date part is correct , but for your code this is invalid.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          desanti wrote:

                          This is correct because the date part is correct , but for your code this is invalid.

                          I wrote it that way; if you want different behaviour, look the method up on MSDN.

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups