DateTime.Parse(string s) DOES NOT WORK!!!

uwking

Newcomer
Joined
Feb 10, 2004
Messages
8
Location
Ontario, Canada
<SIZE=5>
Hello gurus,

I have been lookin at this for like 6 hours now, and I am turnin to you guys...

This below is the error I get

<B> --- ERROR BEGINS --- </B>

String was not recognized as a valid DateTime.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error:


Line 248:
Line 249: string myDateTimeValue = "2/16/1992 12:15:12";
Line 250: DateTime myDateTime = DateTime.Parse (myDateTimeValue);
Line 251:
Line 252:


Source File: c:\inetpub\wwwroot\agentaccountingapp\agent\store_usage.aspx.cs Line: 250

Stack Trace:


[FormatException: String was not recognized as a valid DateTime.]
System.DateTimeParse.GetDayOfNNY(DateTimeResult result, DateTimeRawInfo raw, DateTimeFormatInfo dtfi) +202
System.DateTimeParse.ProcessTerminaltState(Int32 dps, DateTimeResult result, DateTimeRawInfo raw, DateTimeFormatInfo dtfi) +142
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +488
System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) +30
System.DateTime.Parse(String s, IFormatProvider provider) +11
System.DateTime.Parse(String s) +7
AgentAccountingApp.Agent.store_usage.lstBoxWeek_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\agentaccountingapp\agent\store_usage.aspx.cs:250
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +108
System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +26
System.Web.UI.Page.RaiseChangedEvents() +115
System.Web.UI.Page.ProcessRequestMain() +1081




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

<B> --- ERROR ENDS --- </B>

By the way, that code is strictly taken from msdn site...
There seems to be nothing wrong with the code itself nor import problems...

I'm totally lost...HELP!!!
</SIZE>
 
try
C#:
             System.IFormatProvider format =
                 new System.Globalization.CultureInfo("en-US", true);
 
             string myDateTimeValue = "2/16/1992 12:15:12";
             DateTime myDateTime = DateTime.Parse (myDateTimeValue,format);
their code is assuming US date / time format. If you are using different regional settings then this will cause your problem.
 
It has something to do with globalization and I think it is expecting a US formatted date because it works fine with

string myDateTimeValue = "16/2/1992 12:15:12";

try setting the IFormatProvider part of the Parse function.
 
Just noticed - of course "2/16/1992" wont work as there are only 12 months in the year. The example is in a US format so just change your string to be UK format and you wont need to add the globalization (unless your from the US).

;-)
 
Thanks a bunch!!!

That fixed it, thanks PlausiblyDamp!!! :D

PlausiblyDamp said:
try
C#:
             System.IFormatProvider format =
                 new System.Globalization.CultureInfo("en-US", true);
 
             string myDateTimeValue = "2/16/1992 12:15:12";
             DateTime myDateTime = DateTime.Parse (myDateTimeValue,format);
their code is assuming US date / time format. If you are using different regional settings then this will cause your problem.
 
Back
Top