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. C#
  4. Problem with Invalid Date

Problem with Invalid Date

Scheduled Pinned Locked Moved C#
csharphelpdatabasequestion
6 Posts 3 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.
  • R Offline
    R Offline
    Reality Strikes
    wrote on last edited by
    #1

    I'm facing some issues with the date thing: While debugging, I can see that the value of stringDate is coming as "3/60/1989" (MM/DD/YYYY), which is practically not possible, but these value is coming from the database as it is wrongly entered there. Now when this stringDate is parsed into DateTime format, it gives me a different date (i.e. the value of dtDate is shown as "6/30/1989") and throws an exception "Invalid DateTime Entry" at this line:

    dtDate = DateTime.Parse(stringDate);

    if (dtDate > startDate && dtDate < endDate)
    {
    row["NEW_DATE_FIELD"] = stringDate;
    myTable.ImportRow(row);
    }

    So, now I want that my code to do the following: To enter a "INVALID DATE" message to the row["NEW_DATE_FIELD"] instead of the wrong date as such Note : In VB.NET we have a built-in method named **IsDate()**, which receives a string as parameter and return True if the string is a date or false. Is there any short-cut method in C# equivalent to IsDate method ? Any help would be appreciated.

    J 1 Reply Last reply
    0
    • R Reality Strikes

      I'm facing some issues with the date thing: While debugging, I can see that the value of stringDate is coming as "3/60/1989" (MM/DD/YYYY), which is practically not possible, but these value is coming from the database as it is wrongly entered there. Now when this stringDate is parsed into DateTime format, it gives me a different date (i.e. the value of dtDate is shown as "6/30/1989") and throws an exception "Invalid DateTime Entry" at this line:

      dtDate = DateTime.Parse(stringDate);

      if (dtDate > startDate && dtDate < endDate)
      {
      row["NEW_DATE_FIELD"] = stringDate;
      myTable.ImportRow(row);
      }

      So, now I want that my code to do the following: To enter a "INVALID DATE" message to the row["NEW_DATE_FIELD"] instead of the wrong date as such Note : In VB.NET we have a built-in method named **IsDate()**, which receives a string as parameter and return True if the string is a date or false. Is there any short-cut method in C# equivalent to IsDate method ? Any help would be appreciated.

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      DateTime.TryParse[^] You should be able to work it out from there ;)

      R 1 Reply Last reply
      0
      • J J4amieC

        DateTime.TryParse[^] You should be able to work it out from there ;)

        R Offline
        R Offline
        Reality Strikes
        wrote on last edited by
        #3

        it's showing the compile error : 'System.DateTime' does not contain a definition for 'TryParse' do i need to add any other namespaces or references.

        A 1 Reply Last reply
        0
        • R Reality Strikes

          it's showing the compile error : 'System.DateTime' does not contain a definition for 'TryParse' do i need to add any other namespaces or references.

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

          TryParse is only available in .NET framework 2.0 or later. If you are using version 1 then you would have to use the DateTime.Parse method and catch the FormatException when your input is invalid. Alan.

          R 1 Reply Last reply
          0
          • A Alan N

            TryParse is only available in .NET framework 2.0 or later. If you are using version 1 then you would have to use the DateTime.Parse method and catch the FormatException when your input is invalid. Alan.

            R Offline
            R Offline
            Reality Strikes
            wrote on last edited by
            #5

            Thanks Alan, but that doesn't gives me solution to my problem. If the date is invalid, i want the row entry to be message like this : "Invalid Date" what shud be done in order to achieve this?

            A 1 Reply Last reply
            0
            • R Reality Strikes

              Thanks Alan, but that doesn't gives me solution to my problem. If the date is invalid, i want the row entry to be message like this : "Invalid Date" what shud be done in order to achieve this?

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

              Something like this then?

              try {
              dtDate = DateTime.Parse(stringDate);
              if (dtDate > startDate && dtDate < endDate) {
              row["NEW_DATE_FIELD"] = stringDate;
              myTable.ImportRow(row);
              }
              } catch (FormatException) {
              row["NEW_DATE_FIELD"] = "Invalid Date";
              myTable.ImportRow(row);
              }

              Alan.

              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