Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to convert string into DateTime

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-05-2009
Member
 
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); 
          }
      }
  }

Reply With Quote
  #2  
Old 08-05-2009
Member
 
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)
Reply With Quote
  #3  
Old 08-05-2009
Member
 
Join Date: May 2008
Posts: 97
Re: How to convert string into DateTime

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

Quote:
Error 1 The name 'ParseExact' does not exist in the current context D:\EFB\DEV\test\ConsoleApplication1\ConsoleApplication1\Program.cs 14 24 ConsoleApplication1
Reply With Quote
  #4  
Old 08-05-2009
Member
 
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.
Reply With Quote
  #5  
Old 08-05-2009
Member
 
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)
Reply With Quote
  #6  
Old 04-03-2010
Member
 
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
Reply With Quote
  #7  
Old 05-03-2010
Member
 
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.
Reply With Quote
  #8  
Old 05-03-2010
Member
 
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)
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 12:25 AM.