Solved

Convert.ToDateTime (string) ---- Easy

Posted on 2006-11-15
10
398 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

636 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