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!!
IndumathyPosted Jun 13, 2007, 9:30 AM
Thanks a tonne ,Alan , for the suggestions.
AlanPosted Jun 13, 2007, 6:18 AM
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.
IndumathyPosted Jun 13, 2007, 5:52 AM
AlanPosted Jun 13, 2007, 5:45 AM
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);
IndumathyPosted Jun 13, 2007, 5:09 AM
but my code works sometime correctly...but at other times, it gives the output with the missing am/pm
AlanPosted Jun 13, 2007, 4:59 AM
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));
IndumathyPosted Jun 13, 2007, 12:19 AM
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.
Scott LyslePosted Jun 12, 2007, 11:50 PM
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