Datetime conversion and parsing

混江龙づ霸主 提交于 2019-11-30 23:35:53

I just rewrite the format string and it resolves the issue. Found that rewriting { } braces resolved it. Please try it.

try doing this like

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

more can be found here http://www.dotnetperls.com/datetime

you can also convert to string to datetome but you have to check ahead of time the the string is date

 DateTime ckDate =Convert.DateTime(yourinputstring);

you can also use parse

DateTime ckDate=DateTime.Parse(yourinputstring);

You have 15 format specifier in your format string, but only 14 parameters. Unless I have missed one, your format string is invalid.

/*
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 ????
);                                    
*/

As far as date it concerned, you haven't shown how you got ckDate, if it's a string with date in it, or is it a DateTime. Also, from parameter list, looks like ckDate is mapped to something that is called "intQuantityTotal".

If ckDate is a string, then use DateTime.TryPrase method to convert it actual DateTime.

If you are converting from string follow this, assuming your input date is in a string variable called "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.
}

Try using DateTime.ParseExact Method

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

I advise you to use DateTimes constructor when possible, and not bother with converting strings to dates.

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

If you still want to start from a string, you need to use variations of DateTime.Parse

The simplest thing would work:

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

in order to explicitly specify the format of the date use DateTime.ParseExact

Since you mentioned that the date string is entered by the user, I would assume you want a feedback loop in case the user enters an malformed date. In this case you should use DateTime.TryParse

Joe

loopedcode's answer is probably right: you don't have a parameter for the strpartyprefix format specifier.

In addition, you should explicitly format ckDate into the format expected by holdbooking.asp. This probably needs to be a culture invariant format.

E.g. if holdbooking.asp is expecting 'yyyy-MM-dd', then replace

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

by:

String.Format(CultureInfo.InvariantCulture, 
              " ...&dtmLine1={7:yyyy-MM-dd}&...", ...,ckDate,...);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!