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. Convert to DateTime

Convert to DateTime

Scheduled Pinned Locked Moved C#
4 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.
  • O Offline
    O Offline
    ongo
    wrote on last edited by
    #1

    Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception. string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString(); Can somebody tell me what am I doing wrong. Thanks Salut!

    S S S 3 Replies Last reply
    0
    • O ongo

      Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception. string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString(); Can somebody tell me what am I doing wrong. Thanks Salut!

      S Offline
      S Offline
      Sven Cipido
      wrote on last edited by
      #2

      Hi, The error you got is : String was not recognised as a valid date. What I had to do is the reformat the string in this string : string text = "2004-11-30 13:05:01"; I'm not sure from where the text value comes. But you could use other wyas to do this.

      1 Reply Last reply
      0
      • O ongo

        Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception. string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString(); Can somebody tell me what am I doing wrong. Thanks Salut!

        S Offline
        S Offline
        sommarafton
        wrote on last edited by
        #3

        Hi, you could do it with the Converter class instead: Convert.ToDateTime(string yourString); or with a customised format Convert.ToDateTime(string yourString, System.IFormatProvider yourFormat); Cheers, sommarafton

        1 Reply Last reply
        0
        • O ongo

          Hi, I need to convert string in specific format to DateTime structure. I tryed following code but it throws exception. string text = "4113013050"; string format = "YMMddHmmss"; DateTime dateTime = DateTime.ParseExact(text,format,null) ; textBox1.Text = dateTime.ToString(); Can somebody tell me what am I doing wrong. Thanks Salut!

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

          That format will never work and I would be surprised if u can find anyway to parse it. Problems: 1: Years cannot be specified by one digit. How is the computer suposed to know if its 2004, or 2014 or even 1914. U need at least 2 digits and better if u use all 4 (2000 should have taught us that by now) 2: Same happens with the hours. No matter if u use the 12 hour AM/PM format or 24 hours, time can get to two digit values (11 PM / 23). Ur parsing would work if u can get ur date to follow a "YYYYMMddHHmmss" pattern and will probably also work with a "YYMMddHHmmss" pattern although i'd go for first.

          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