日期时间转换和解析

我的代码给出以下错误: 输入字符串格式不正确。

有这个提示:

将字符串转换为DateTime时,解析字符串以在将每个变量放入DateTime对象之前获取日期

下面是我的代码:( 编辑只显示有问题的代码)

string username ="aits"; string password = "12345"; Int32 intresortid=7; string strPartySurname = "Kumar"; ; string strPartyFirstName = "Test"; string strPartyPrefix = "Mr & Mrs"; Int16 intQtyAdults=1; Int16 intQtyTotal=1; string PromotionCode = "TIF"; string flightNO = "FlighDJ76"; string strNotes = "Provide BreakfastHoneymooners"; try { string url = string.Format("http://localhost/insHold.asp?username={0}&password={1‌​}&intresortid={2}&strPartySurname={3}&strPartyFirstName={4}&strPartyPrefix={5}&intQtyAdults={6}&intQtyTotal={7}&dtmLine1={8:yyyy-MM-dd}&strRoomType1={9}&intRooms1={10}&intnights1={11}&strFlightNo={12}&strNotes={13}&strBookingCode={14}&strPromotionCode={15}", username, password, intresortid, strPartySurname, strPartyFirstName, strPartyPrefix, intQtyAdults, intQtyTotal, CheckInDate, BBRoom, RoomQty, NoofNights, flightNO, strNotes, ResBookingID, PromotionCode); WebRequest request = HttpWebRequest.Create(url); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string urlText = reader.ReadToEnd(); bookid = Convert.ToInt16(urlText); } catch (System.ApplicationException ex) { throw ex; } 

我不知道如何更正此作为我的DateTime值:CheckInDate已经是类型日期。

有人可以告诉我如何解决这个问题或指出我正确的方向。

我只是重写格式字符串,它解决了问题。 发现重写{}括号解决了它。 请试一试。

试着这样做

 DateTime ckDate = new DateTime(2012, 09, 24); 

更多信息可以在这里找到http://www.dotnetperls.com/datetime

你也可以转换为字符串到日期,但你必须提前检查字符串是否为日期

  DateTime ckDate =Convert.DateTime(yourinputstring); 

你也可以使用解析

 DateTime ckDate=DateTime.Parse(yourinputstring); 

格式字符串中有15个格式说明符,但只有14个参数。 除非我错过了一个,否则你的格式字符串无效。

 /* username={0}& ->username, password={1‌​}& -> password, intResortID={2}& -> intresortid, strpartysurname={3}& -> strPartySurname, strpartyfirstname={4}& ->strPartyFirstName, strpartyprefix={5}& -> intQuantityTotal, intQuantityTotal={6}& -> ckDate, dtmLine1={7}& -> BBRoom, strRoomType1={8}& -> RoomQty, intRooms1={9}& -> NoofNights, intnights1={10}& -> flightNO, strFlightNo&{11}& -> strNotes, strNotes{12}& -> ResBookingID, strBookingCode{13}& -> PromotionCode strPromotionCode={14}", -> ????? Missing ???? ); */ 

就它所涉及的日期而言,你没有展示你是如何得到ckDate ,如果它是一个带有日期的string ,或者它是一个DateTime 。 此外,从参数列表中,看起来ckDate映射到名为“ intQuantityTotal ”的内容。

如果ckDate是一个字符串,则使用DateTime.TryPrase方法将其转换为实际的DateTime。

如果要从字符串转换,请按照此操作,假设您的输入日期位于名为“ inputDate ”的字符串变量中:

 DateTime checkInDate; if (!DateTime.TryParse(inputDate, out checkInDate)) { //This is error condition, which means your string date wasn't convertible to DateTime } else { // Variable checkInDate now contains converted DateTime. // You can put your format string here and your DateTime should work fine. } 

尝试使用DateTime.ParseExact Method

 DateTime result = DateTime.ParseExact("2012-09-24", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); 

我建议你尽可能使用DateTimes构造函数,而不是将字符串转换为日期。

 DateTime t = new DateTime(2012, 9, 24); 

如果您仍想从字符串开始,则需要使用DateTime.Parse的变体

最简单的方法是:

 DateTime t = DateTime.Parse("2012-9-24"); 

为了明确指定日期的格式,请使用DateTime.ParseExact

由于您提到用户输入了日期字符串,因此我假设您需要一个反馈循环,以防用户输入格式错误的日期。 在这种情况下,您应该使用DateTime.TryParse

循环代码的答案可能是正确的:你没有strpartyprefix格式说明符的参数。

此外,您应该将ckDate显式格式化为ckDate的格式。 这可能需要是一种文化不变的格式。

例如,如果holdbooking.asp期待’yyyy-MM-dd’,那么替换

 String.Format(" ...&dtmLine1={7}&...", ...,ckDate,...); 

通过:

 String.Format(CultureInfo.InvariantCulture, " ...&dtmLine1={7:yyyy-MM-dd}&...", ...,ckDate,...);