Link to home
Start Free TrialLog in
Avatar of JohnnyKnoxville
JohnnyKnoxville

asked on

Convert.ToDateTime (string) ---- Easy

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!!!!
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

string s = "11/8/2006 5:02:2006 PM";
DateTime d = DateTime.ParseExact(s,"dd/M/yyyy h:mm:ss tt", null);
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.
Avatar of JohnnyKnoxville
JohnnyKnoxville

ASKER

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.
>> Im confused....why would you think that is a typo?  
You have 2006 for the seconds : 5:02:2006.
>>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
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???
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Im brand new to c#.  Can you show me how?

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