Solved

Convert.ToDateTime (string) ---- Easy

Posted on 2006-11-15
10
387 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now