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
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. datetime.tryparse("datetime in dd/MM/yyyy format",datetimeobject) returns false for dd/mm/yyyy

datetime.tryparse("datetime in dd/MM/yyyy format",datetimeobject) returns false for dd/mm/yyyy

Scheduled Pinned Locked Moved .NET (Core and Framework)
help
18 Posts 9 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.
  • S schampacc

    I have requirement where user can pass all types of datetime formats. Also I need to validate all the datetime formats entered by user.

    DateTime.TryParse() returns false, if the date passed is in dd/MM/yyyy format. I have noticed in all the forums this issue is been noted.

    Can anyone reply why this error is been thrown and whats the solution so as to pass all datetime formats

    A Offline
    A Offline
    Alan N
    wrote on last edited by
    #3

    schampacc wrote:

    I have requirement where user can pass all types of datetime formats.

    Forget about the code for a moment and think about this. What is meant by 1) 04/05/2011 2) 05/04/2011 Both could mean either 4 May 2011 or 5 April 2011 and it's likely that even the Startrek Universal Translator cannot solve this fundamental problem. Alan.

    S 1 Reply Last reply
    0
    • A Alan N

      schampacc wrote:

      I have requirement where user can pass all types of datetime formats.

      Forget about the code for a moment and think about this. What is meant by 1) 04/05/2011 2) 05/04/2011 Both could mean either 4 May 2011 or 5 April 2011 and it's likely that even the Startrek Universal Translator cannot solve this fundamental problem. Alan.

      S Offline
      S Offline
      schampacc
      wrote on last edited by
      #4

      It returns true for 05/04/2011 but returns false for 25/04/2011

      M A 2 Replies Last reply
      0
      • S schampacc

        It returns true for 05/04/2011 but returns false for 25/04/2011

        M Offline
        M Offline
        MicroVirus
        wrote on last edited by
        #5

        And does it return true for both 04/05/2011 and 04/25/2011? How the date/time is interpreted depends on the locale used. This is good, cf. Star Trek Universal Translator in previous post.

        S 1 Reply Last reply
        0
        • M MicroVirus

          And does it return true for both 04/05/2011 and 04/25/2011? How the date/time is interpreted depends on the locale used. This is good, cf. Star Trek Universal Translator in previous post.

          S Offline
          S Offline
          schampacc
          wrote on last edited by
          #6

          yes it returns true for both the cases, since both will be considered as MM/dd/yyyy

          1 Reply Last reply
          0
          • S schampacc

            It returns true for 05/04/2011 but returns false for 25/04/2011

            A Offline
            A Offline
            Alan N
            wrote on last edited by
            #7

            schampacc wrote:

            It returns true for 05/04/2011 but returns false for 25/04/2011

            With that information I can deduce that your system's Culture uses MM/dd/yyyy format and it shouldn't be a surprise that TryParse rejects a month number of 25. When the day number is less than 13 it is not possible to understand a date without prior knowledge of the Culture. Therefore I can guess that "25/04/2011" is 25 April 2011 but I can't say whether "05/04/2011" is 5 April or 4 May unless I know the field order within the string. Alan.

            1 Reply Last reply
            0
            • S schampacc

              I have requirement where user can pass all types of datetime formats. Also I need to validate all the datetime formats entered by user.

              DateTime.TryParse() returns false, if the date passed is in dd/MM/yyyy format. I have noticed in all the forums this issue is been noted.

              Can anyone reply why this error is been thrown and whats the solution so as to pass all datetime formats

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #8

              schampacc wrote:

              pass all types of datetime formats

              That isn't possible. Different cultures expect different forms of dates and those forms are not deterministically unique without additional information. So either you must limit the possible forms or you must provide a way for the user (or user app) to tell you what form is expected. There is no other possibility.

              S 1 Reply Last reply
              0
              • J jschell

                schampacc wrote:

                pass all types of datetime formats

                That isn't possible. Different cultures expect different forms of dates and those forms are not deterministically unique without additional information. So either you must limit the possible forms or you must provide a way for the user (or user app) to tell you what form is expected. There is no other possibility.

                S Offline
                S Offline
                schampacc
                wrote on last edited by
                #9

                Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.

                DateTimeStyles styles = DateTimeStyles.None;
                DateTime startDate;
                foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
                {
                DateTime.TryParse("stringDate", cInfo, styles, out startDate)
                }

                D P J 3 Replies Last reply
                0
                • S schampacc

                  Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.

                  DateTimeStyles styles = DateTimeStyles.None;
                  DateTime startDate;
                  foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
                  {
                  DateTime.TryParse("stringDate", cInfo, styles, out startDate)
                  }

                  D Offline
                  D Offline
                  Daniel Grondal
                  wrote on last edited by
                  #10

                  Will you still not have problems when the day number is less than 13? 04/05/11 and 05/04/11 will probably be ok with several cultures?

                  //daniel

                  modified on Tuesday, September 13, 2011 5:06 AM

                  S 1 Reply Last reply
                  0
                  • D Daniel Grondal

                    Will you still not have problems when the day number is less than 13? 04/05/11 and 05/04/11 will probably be ok with several cultures?

                    //daniel

                    modified on Tuesday, September 13, 2011 5:06 AM

                    S Offline
                    S Offline
                    schampacc
                    wrote on last edited by
                    #11

                    Thats right. Then in this case one should go for one particular format only. The issue was to validate the date is in valid dateformat or not. It can be in any valid dateformat. This issue can be solved using the above loop.

                    D 1 Reply Last reply
                    0
                    • S schampacc

                      Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.

                      DateTimeStyles styles = DateTimeStyles.None;
                      DateTime startDate;
                      foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
                      {
                      DateTime.TryParse("stringDate", cInfo, styles, out startDate)
                      }

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #12

                      This is not the way to solve this - in fact, it's an incredibly naive solution. As an example, 9/11/2001 - what happened on that day? It was either a terrible disaster in America or a very quiet day in November. You should use the universal datetime format in your application, which is agnostic of ALL cultures.

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                      S 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        This is not the way to solve this - in fact, it's an incredibly naive solution. As an example, 9/11/2001 - what happened on that day? It was either a terrible disaster in America or a very quiet day in November. You should use the universal datetime format in your application, which is agnostic of ALL cultures.

                        Forgive your enemies - it messes with their heads

                        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                        S Offline
                        S Offline
                        schampacc
                        wrote on last edited by
                        #13

                        Do you mean to define the datetime object as DateTime.UTC?

                        P 1 Reply Last reply
                        0
                        • S schampacc

                          Thats right. Then in this case one should go for one particular format only. The issue was to validate the date is in valid dateformat or not. It can be in any valid dateformat. This issue can be solved using the above loop.

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #14

                          schampacc wrote:

                          Then in this case one should go for one particular format only.

                          And your test still doesn't hold up. Take a look at 4/20/2011. Is that a valid date of the form April 20th or an invalid date when someone tried to enter the 4th day of the 20th month?

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak

                          1 Reply Last reply
                          0
                          • S schampacc

                            Do you mean to define the datetime object as DateTime.UTC?

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #15

                            I mean that the date should conform to ISO 8601.

                            Forgive your enemies - it messes with their heads

                            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                            P 1 Reply Last reply
                            0
                            • S schampacc

                              Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.

                              DateTimeStyles styles = DateTimeStyles.None;
                              DateTime startDate;
                              foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
                              {
                              DateTime.TryParse("stringDate", cInfo, styles, out startDate)
                              }

                              J Offline
                              J Offline
                              jschell
                              wrote on last edited by
                              #16

                              schampacc wrote:

                              I have found the solution to support all datetime formats.

                              No you haven't. I said it was impossible - which it is. There is no solution. So whatever you are doing is certainly not a solution. What you are probably doing is testing within a LIMITED cultural scope. Which might be what you need to do but isn't what you asked. And if it isn't what you need to do then your solution will fail.

                              1 Reply Last reply
                              0
                              • P Pete OHanlon

                                I mean that the date should conform to ISO 8601.

                                Forgive your enemies - it messes with their heads

                                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #17

                                Pete O'Hanlon wrote:

                                conform to ISO 8601

                                Hear hear! I'm glad I didn't have to say it this time. :thumbsup:

                                D 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  Pete O'Hanlon wrote:

                                  conform to ISO 8601

                                  Hear hear! I'm glad I didn't have to say it this time. :thumbsup:

                                  D Offline
                                  D Offline
                                  DaveyM69
                                  wrote on last edited by
                                  #18

                                  :laugh:

                                  Dave
                                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                                  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