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

regex

Scheduled Pinned Locked Moved C#
regex
5 Posts 4 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.
  • A Offline
    A Offline
    arkiboys
    wrote on last edited by
    #1

    Hello,
    I would like to validate strings to datetime format using regex please

    Richard DeemingR K 2 Replies Last reply
    0
    • A arkiboys

      Hello,
      I would like to validate strings to datetime format using regex please

      Richard DeemingR Online
      Richard DeemingR Online
      Richard Deeming
      wrote on last edited by
      #2

      Not with regular expressions! :laugh: Use DateTime.TryParseExact[^] to validate the string against your required format. However, there's a small problem. Your "valid" string doesn't actually match the format you're validating against. Your format requires the letter "T" between the date and time, but neither of your strings have that.

      string input = "2017-09-19T08:04:31.123";

      DateTime parsedDate;
      bool isValid = DateTime.TryParseExact(input,
      "yyyy-MM-ddThh:mm:ss.fff",
      CultureInfo.InvariantCulture,
      DateTimeStyles.AssumeUniversal,
      out parsedDate);

      /*
      isValid: true
      parsedDate: 2017-09-19 09:04:31.123 UTC
      */


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • A arkiboys

        Hello,
        I would like to validate strings to datetime format using regex please

        K Offline
        K Offline
        Kenneth Haugland
        wrote on last edited by
        #3

        DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture);

        B 1 Reply Last reply
        0
        • K Kenneth Haugland

          DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture);

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          In practice, you would wrap a call to DateTime.ParseExact in a try/catch block ... because it will throw an error if it fails.

          «While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)

          Richard DeemingR 1 Reply Last reply
          0
          • B BillWoodruff

            In practice, you would wrap a call to DateTime.ParseExact in a try/catch block ... because it will throw an error if it fails.

            «While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)

            Richard DeemingR Online
            Richard DeemingR Online
            Richard Deeming
            wrote on last edited by
            #5

            Better to use TryParseExact, which won't. :-D


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            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