简体   繁体   English

字符串未被识别为有效的DateTime

[英]String was not recognized as a valid DateTime

In my Web Project I am Parsing the Date selected in the Date Picker and comparing it with the Date fetched from Database till Yesterday it was working fine but from today morning it throws the Format Exception what could be the Problem, 在我的Web项目中,我正在解析在“日期选择器”中选择的日期,并将其与从数据库中获取的日期进行比较,直到昨天为止一切正常,但是从今天早上开始,它会引发“ 格式异常” ,可能是问题所在,

My code is, 我的代码是

  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;
        }

Today is the 13th of June 2013. It was probably working before because the day was 12 or less. 今天是2013年6月13日。它可能在工作之前,因为当天是12天或更短。 You need to ensure the format of the date coming from the picker is the same as the format expected when parsing. 您需要确保来自选择器的日期格式与解析时预期的格式相同。

Currently, it looks like it's parsing the day as the month. 当前,它似乎将日期解析为月份。

Be explicit on the CultureInfo, based on the date format you're expecting to parse 根据您希望解析的日期格式,在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);

try like this,place code 尝试这样,放置代码

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM