954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convert String to Datetime?

I have a String stored in this format "10/10/2009 12:00:00".
I would like to convert this into a DateTime object so I can add it into the Outlook calender.
I have tried using the following code but it doesnt seem to work.
DateTime startDate = DateTime.Parse(appointments[i]);
(where appointments[i] is a string in a point in an array)

I have also tried using the following code to parse butit hasnt lead to anything for me:
System.Globalization.DateTimeFormatInfo info = new System.Globalization.DateTimeFormatInfo();

Can somebody show me how to parse the above format or by another method i cant seem to get to work either way. Many thanks

Drahmina
Newbie Poster
6 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 
DateTime datetime = DateTime.ParseExact("10/10/2009 12:00:00", "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);


Also, have a look at this: http://msdn.microsoft.com/en-us/library/system.datetimeoffset.tryparseexact.aspx

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

Thanks very much for the information:)

Drahmina
Newbie Poster
6 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

I have tried this but it still doesnt seem to work correctly. In the database, i have s string stored as "10/11/2009 13:00:00" which is the date.

The code below is what i use to read from the database butonly the first line works...

string temps = reader.GetString(4); 
DateTime datetimeStart = DateTime.ParseExact (temps, "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);


Could you point me as to where im going wrong or a better way to achieve what im trying to do?

Drahmina
Newbie Poster
6 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

What error do you get? The code looks right so it should convert the string.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Hi,

Could you also give a try like this way;

string temps = "10/11/2009 13:00:00";
DateTime datetimeStart = Convert.ToDateTime(temps, System.Globalization.CultureInfo.CurrentCulture);


Good luck.

MeSampath
Junior Poster
102 posts since Oct 2009
Reputation Points: 23
Solved Threads: 27
 

Are you shure that appointments[i] is the correct string. I tried DateTime.Parse("10/10/2009 12:00:00") and it works fine.

koleraba
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

DateTime datetime = DateTime.ParseExact("10/10/2009 12:00:00", "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

Assuming the 10/10/2009 is day/month/year and really, the above solutions should work just fine.

Ravenheart
Light Poster
27 posts since Nov 2009
Reputation Points: 11
Solved Threads: 5
 

DateTime datetime = DateTime.ParseExact("10/10/2009 12:00:00", "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

Assuming the 10/10/2009 is day/month/year and really, the above solutions should work just fine.


You mean month/day/year, surely? :)

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Err yes sorry :)

Ravenheart
Light Poster
27 posts since Nov 2009
Reputation Points: 11
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: