Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert.ToDateTime (string) ---- Easy

Posted on 2006-11-15
10
391 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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