c# - 日期时间转换和解析

标签 c# parsing datetime user-input

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

有了这个提示:

将字符串转换为 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 = "<nl>Provide Breakfast<nl>Honeymooners";

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

关于c# - 日期时间转换和解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12417770/

相关文章:

parsing - Haskell:let 语句有问题

Java 7 SimpleDateformat 滥用或问题?

python - 使用 ElementTree 解析 XML

Python 日期时间 : strftime() codes for decimal numbers

c# - 在 C# 控制台应用程序中运行 NUnit 测试

c# - appsettings.json 中的 ASP.NET Core 强类型选项

python-2.7 - 我可以对自变量进行数学计算,以便在 Gnuplot 中的 x 轴上显示吗?

python - python中从下一个时间值中减去上一个时间值

c# - 将文本文件添加到我的代码的开头会导致解析错误

c# - OpenFileDialog.ShowDialog() 在 Silverlight 中引发 InvalidOperationException