Results 1 to 8 of 8

Thread: How to convert string into DateTime

  1. #1
    Join Date
    May 2008
    Posts
    97

    How to convert string into DateTime

    I want to convert a string such as "YYYYMMDD" or "DDMMYYYY" in DateTime object.

    According to MSDN, the method Parse (string) can do this changes but I can not make it work.

    Here is my code:

    Code:
      using System; 
      using System. Collections. Generic; 
      using System. Text; 
      using System. Globalization; 
    
      namespace ConsoleApplication1 
      {
          class Program 
          {
              static void Main (string [] args) 
              {
                  string date = "041110"; 
                  DateTime datetest = new DateTime () ;
                  datetest = Parse (date); 
              }
          }
      }

  2. #2
    Join Date
    May 2008
    Posts
    3,971

    Re: How to convert string into DateTime

    Try it with ParseExact.

    Code:
    datetest = DateTime.ParseExact (date, "YYYYMMDD", null)

  3. #3
    Join Date
    May 2008
    Posts
    97

    Re: How to convert string into DateTime

    Its still not working. I get the below error message:

    Error 1 The name 'ParseExact' does not exist in the current context D:\EFB\DEV\test\ConsoleApplication1\ConsoleApplication1\Program.cs 14 24 ConsoleApplication1

  4. #4
    Join Date
    May 2008
    Posts
    685

    Re: How to convert string into DateTime

    Certainly the format of your date is not recognized as valid by your current system. You just test this

    Code:
      string [] info = DateTimeFormatInfo. CurrentInfo. GetAllDateTimePatterns 
      foreach (string s in info) 
       { Console. WriteLine (s) ; }
    And you will have the opportunity to see that your format is not.

  5. #5
    Join Date
    May 2008
    Posts
    3,971

    Re: How to convert string into DateTime

    The problem is that for DateTimeFormatInfo the mask of a recognized format comprises a separator not empty:

    Code:
      "MM-dd-yyyy": recognized 
      "dd45 *** 6MM45 *** 6yyyy": recognized (DateSeparator = "45 *** 6") 
      "DDMMYYYY": not recognized (even by defining DateSeparator = String.Empty)

  6. #6
    Join Date
    Mar 2010
    Posts
    2

    Re: How to convert string into DateTime

    Quote Originally Posted by switchblade327 View Post
    The problem is that for DateTimeFormatInfo the mask of a recognized format comprises a separator not empty:

    Code:
      "MM-dd-yyyy": recognized 
      "dd45 *** 6MM45 *** 6yyyy": recognized (DateSeparator = "45 *** 6") 
      "DDMMYYYY": not recognized (even by defining DateSeparator = String.Empty)
    Or ... you could use the correct formatting: "ddMMyyyy"
    which will compile

  7. #7
    Join Date
    Nov 2009
    Posts
    446

    Re: How to convert string into DateTime

    Hello,
    Have a look at the following code, this may help you.
    Code:
    string myDateString = "some text here";
    DateTimeFormatInfo dtfr = new DateTimeFormatInfo();
    dtfr.ShortDatePattern = "ddMMyyyy";
    DateTime d = DateTime.Parse(myDateString, dtfr);
    Hope the above part of the code will work for you.

  8. #8
    Join Date
    Mar 2010
    Posts
    2

    Re: How to convert string into DateTime

    It wasn't really a question merely stating that in the code

    Code:
    "DDMMYYYY": not recognized (even by defining DateSeparator = String.Empty)
    the problem wasn't the DateSeparator but the use of an incorrect dateformat.

    my solution was:
    Code:
    string myDate = "20100303";
    string format = "yyyyMMdd";
    DateTime date = DateTime.ParseExact(myDate, format, null);
    which will parse the date (if it is a date in this format)

Similar Threads

  1. Convert string into int in C
    By screwball in forum Software Development
    Replies: 4
    Last Post: 22-12-2011, 08:47 PM
  2. How to convert DateTime to Date and vice versa in c#?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 09:35 PM
  3. DateTime Format String
    By Zool in forum Software Development
    Replies: 3
    Last Post: 17-07-2009, 03:20 PM
  4. Convert datetime to small date in C#
    By Dakshesh in forum Software Development
    Replies: 3
    Last Post: 09-06-2009, 11:49 AM
  5. How to convert string to int
    By Zavier in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 06:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,544,098.07024 seconds with 17 queries