Solved

Convert.ToDateTime (string) ---- Easy

Posted on 2006-11-15
10
390 Views
Last Modified: 2012-05-05
Im still pretty new to C#.   How do I get this to work?

I have a string in this format...

"11/8/2006 5:02:2006 PM"

and need to convert this string to a date/time format.


I tried this...

MyClass.MyClassProperty = Convert.ToDateTime("11/8/2006 5:02:2006 PM",DateTimeFormatInfo.CurrentInfo);

But its not working...

I just need to convert it from a string to date/time.

thx!!!!
0
Comment
Question by:JohnnyKnoxville
  • 7
  • 3
10 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17949680
string s = "11/8/2006 5:02:2006 PM";
DateTime d = DateTime.ParseExact(s,"dd/M/yyyy h:mm:ss tt", null);
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17949698
It's a bit hard to tell from your example but the format I have provided is as follows :

dd/M/yyyy h:mm:ss tt

dd = two digit day with leading zero
M = single or double digit month with no leading zero
yyyy = four digit year

h = single digit hour in 12-hour clock format with no leading zero
mm = double digit minute with leading zero
ss : double digit seconds with leading zero !!! NB : you see to have 2006 here, is that a typo !!!!

tt = AP or PM.
0
 

Author Comment

by:JohnnyKnoxville
ID: 17950518
Sorry...my format is mm/dd/yyyy hh:mm:ss tt


Im confused....why would you think that is a typo?  

the year is in a 4 digit format (2006).

When I use this line of code...

MyClass.MyClassProperty = Convert.ToDateTime("11/8/2006 5:02:2006 PM",DateTimeFormatInfo.CurrentInfo);

MyClassProperty is equal to 11/8/2006.....but the time is not included.  I'll try the ParseExact but my mm/dd are inverted so I doubt it'll work for me.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 15

Expert Comment

by:ozymandias
ID: 17950613
>> Im confused....why would you think that is a typo?  
You have 2006 for the seconds : 5:02:2006.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17950633
>>Sorry...my format is mm/dd/yyyy hh:mm:ss tt

No it isn't : you have a single digit day : 8...it should be 11/08/2006
0
 

Author Comment

by:JohnnyKnoxville
ID: 18132574
sorry for the delay with this.  Ive been sidetracked with a bazillion other things.

Ok....my format is ...

mm/D/yyyy hh:mm:ss:tt

11/8/2006 5:02:2006 PM

but it could also be...

11/18/2006 5:02:2006 PM

How do I accommodate a single digit day and two digit day format???
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 50 total points
ID: 18133124
try both.

Use ParseExact with the double digit day inside a try/catch block.
It if fails catch the error and try with the single digit day.
If that fails throw the error.
0
 

Author Comment

by:JohnnyKnoxville
ID: 18133684
Im brand new to c#.  Can you show me how?

thx
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 18134672
string format1 = "mm/D/yyyy hh:mm:ss tt"; // single digit month
string format2 = "mm/D/yyyy hh:mm:ss tt"; // double digit month
string s = "your date string in either format";

DateTime dt;

try{
    td = DateTime.ParseExact(s, fomat1, null); // try to parse single digit month
}catch{
    // oh no...looks like that didn't work so lets try again
    td = DateTime.ParseExact(s, format2, null); // try to parse double digit month
    // if this fails (i.e. if s does not match either string an error will be thrown here
    // we could enclose the second attempt in another try/catch block if we want
}

// if we make it to here dt will now hold a sucessfully parsed DateTime value.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 18134685
sorry, error above.

The first two lines should be :

string format1 = "MM/d/yyyy hh:mm:ss tt"; // single digit day
string format2 = "MM/dd/yyyy hh:mm:ss tt"; // double digit day
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question