[英]String was not recognized as a valid DateTime
在我的Web項目中,我正在解析在“日期選擇器”中選擇的日期,並將其與從數據庫中獲取的日期進行比較,直到昨天為止一切正常,但是從今天早上開始,它會引發“ 格式異常” ,可能是問題所在,
我的代碼是
try
{
ReportDocument rpt = new ReportDocument();
DateTime dt = DateTime.Parse(frmtxtdt.Text); // Exception Thrown here
DateTime dt1 = DateTime.Parse(frmtxtdt.Text);
DateTime date = DateTime.Parse(DateTime.Today.ToShortDateString());
DateTime date1 = DateTime.Parse(DateTime.Today.ToShortDateString());
string frtxt = String.Format("{0:MM-dd-yyyy}", dt);
string totxt = String.Format("{0:MM-dd-yyyy}", dt1);
DataSet ds = Namespace.SP.Storedprocedure(frtxt,totxt).GetDataSet();
if (!IsPageRefresh)
{
if (ds.Tables[0].Rows.Count > 0)
{
if(frtxt == ds.Tables[0].Rows[0]["Date"].ToString()
&& totxt == ds.Tables[0].Rows[0]["Date"].ToString())
{
ds.Tables[0].TableName = "Passkeys";
ds.WriteXml(Server.MapPath("~/XML/Passkeys.xml"));
string filename = Server.MapPath("~/Upload/Pkey_rpt.rpt");
rpt.Load(filename);
rpt.SetDataSource(ds);
rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Passkeys - " + ds.Tables[0].Rows[0]["Date"].ToString());
}
}
else if(frmtxtdt.Text.Trim() !=null && totxtdt.Text.Trim()!=null)
{
if (frtxt == String.Format("{0:dd-MM-yyyy}", date)
&& totxt == String.Format("{0:dd-MM-yyyy}", date1)
&& ds.Tables[0].Rows.Count == 0)
{
ClientMessaging( "Pass Key(s) Not Yet Delivered for the Selected Date...");
}
else
{
ClientMessaging( "There is No Schedule for the Selected date....");
}
}
}
}
catch (Exception ex)
{
lblmsg.Text = ex.Message;
}
今天是2013年6月13日。它可能在工作之前,因為當天是12天或更短。 您需要確保來自選擇器的日期格式與解析時預期的格式相同。
當前,它似乎將日期解析為月份。
根據您希望解析的日期格式,在CultureInfo上明確顯示
CultureInfo GBCultureInfo = new CultureInfo("en-GB"); // dd/MM/YYYY
// CultureInfo USCultureInfo = new CultureInfo("en-US"); // MM/dd/YYYY
dt = DateTime.Parse(frmtxtdt.Text,GBCultureInfo);
嘗試這樣,放置代碼
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");//you can change to any country
ReportDocument rpt = new ReportDocument();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.