I tried using the function convert.todatetime("06/12/2007 07:20:39 AM").
The result i got was 6/12/2007 7:20:39 .
The error is : the AM/PM part is missing.
My culture is set to neutral.
What could be the possible error?
This issue calls for urgent corrections.Please help.
Thanks in advance!!

Scott LysleRank #49 · 31K reputationPosted Jun 12, 2007, 7:50 PM EDT
How you have set the time is fine, the missing AM or PM is likely due to how you are formatting the date for display; here are some examples:
DateTime dt = Convert.ToDateTime("06/12/2007 7:20:39 AM");
MessageBox.Show(dt.ToString()); // displays time as 7:20:39 AM
MessageBox.Show(dt.ToString("hh:mm:ss tt")); // displays time as 07:20:39 AM
MessageBox.Show(dt.ToString("h:mm:ss tt")); // displays time as 7:20:39 AM
MessageBox.Show(dt.ToString("hh:mm:ss")); // displays time as 07:20:39 - 24 hour (Military) time
MessageBox.Show(dt.ToShortTimeString()); // displays time as 7:20 AM
Indumathy18 reputationPosted Jun 12, 2007, 8:19 PM EDT
tempDateTime = TCADBReader.GetString(0);
debug1.WriteLine("{0} >> StartDateTime (String) :: {1}", DateTime.Now, tempDateTime);
StartDateTime = Convert.ToDateTime(tempDateTime);
//StartDateTime = TCADBReader.GetDateTime(0);
debug1.WriteLine("{0} >> StartDateTime " +
": {1}",DateTime.Now,StartDateTime);
Output -
6/11/2007 4:40:00 PM >> StartDateTime (String) :: 06/12/2007 07:20:39 AM
6/11/2007 4:40:00 PM >> StartDateTime : 6/12/2007 7:20:39
can u see the am/pm part missing??? is it compulsory that i use a formatter??
The point it ... the above code doesnot throw an exception but simply gives the output without an am/pm.
AlanRank #669 · 3.6K reputationPosted Jun 13, 2007, 12:59 AM EDT
When you use the Debug.WriteLine() method with a DateTime value (dt say), what gets displayed is dt.ToString(), which represents the default DateTime format for the current culture. In the culture you're using (nor in mine for that matter) that doesn't include AM/PM.
This applies regardless of how the DateTime value was obtained in the first place, whether from parsing a string or otherwise.
If you need, DateTimes to be displayed in a fixed format, then the best option is to define the format you need and always use that. As Scott indicated, all you need to do is to include the format as a parameter to the ToString() method:
StartDateTime = Convert.ToDateTime(tempDateTime);
string format = "MM/dd/yyyy hh:mm:ss tt";
debug1.WriteLine("{0} >> StartDateTime " + ": {1}", DateTime.Now.ToString(format), StartDateTime.ToString(format));
Indumathy18 reputationPosted Jun 13, 2007, 1:09 AM EDT
but my code works sometime correctly...but at other times, it gives the output with the missing am/pm
AlanRank #669 · 3.6K reputationPosted Jun 13, 2007, 1:45 AM EDT
What do you get if you try this code:
using System.Globalization;
.......
DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
Debug.WriteLine(dtfi.AMDesignator);
Debug.WriteLine(dtfi.PMDesignator);
Indumathy18 reputationPosted Jun 13, 2007, 1:52 AM EDT
AlanRank #669 · 3.6K reputationPosted Jun 13, 2007, 2:18 AM EDT
http://msdn2.microsoft.com/en-us/library/8kb3ddd4(vs.80).aspx
To make sure it's not a problem with the debugger, I'd try running the app in release mode.
Failing that, all I can suggest is that you try working in a different culture such as en-US.
Indumathy18 reputationPosted Jun 13, 2007, 5:30 AM EDT
Thanks a tonne ,Alan , for the suggestions.