如何将其转换为单个DateTime变量? [英] How can I transform this into a single DateTime variable?

查看:70
本文介绍了如何将其转换为单个DateTime变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我一直试图这样做一段时间请帮忙...



来自这个HTML:



Hey guys, I have been trying to do this for a while please help...

from this html :

<div>
            <p>
                2.4. Event/Meeting start date and time (*):
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" SetFocusOnError="true" runat="server" ControlToValidate="eventStartDate" ForeColor="red" ErrorMessage="<- Required"></asp:RequiredFieldValidator>
            </p>
            <asp:TextBox ID="eventStartDate" runat="server" Width="100px"></asp:TextBox>
            <ajaxToolkit:CalendarExtender ID="eventStartDate_CalendarExtender" Format="dd-MM-yyyy" TargetControlID="eventStartDate"

                runat="server">
            </ajaxToolkit:CalendarExtender>
            at
        <asp:DropDownList ID="eventStartHour" runat="server">
            <asp:ListItem>--</asp:ListItem>
            <asp:ListItem>00</asp:ListItem>
            <asp:ListItem>01</asp:ListItem>
            <asp:ListItem>02</asp:ListItem>
            <asp:ListItem>03</asp:ListItem>
            <asp:ListItem>04</asp:ListItem>
            <asp:ListItem>05</asp:ListItem>
            <asp:ListItem>06</asp:ListItem>
            <asp:ListItem>07</asp:ListItem>
            <asp:ListItem>08</asp:ListItem>
            <asp:ListItem>09</asp:ListItem>
            <asp:ListItem>10</asp:ListItem>
            <asp:ListItem>11</asp:ListItem>
            <asp:ListItem>12</asp:ListItem>
            <asp:ListItem>13</asp:ListItem>
            <asp:ListItem>14</asp:ListItem>
            <asp:ListItem>15</asp:ListItem>
            <asp:ListItem>16</asp:ListItem>
            <asp:ListItem>17</asp:ListItem>
            <asp:ListItem>18</asp:ListItem>
            <asp:ListItem>19</asp:ListItem>
            <asp:ListItem>20</asp:ListItem>
            <asp:ListItem>21</asp:ListItem>
            <asp:ListItem>22</asp:ListItem>
            <asp:ListItem>23</asp:ListItem>
        </asp:DropDownList>
            :
        <asp:DropDownList ID="eventStartMinute" runat="server">
            <asp:ListItem>--</asp:ListItem>
            <asp:ListItem>00</asp:ListItem>
            <asp:ListItem>05</asp:ListItem>
            <asp:ListItem>10</asp:ListItem>
            <asp:ListItem>15</asp:ListItem>
            <asp:ListItem>20</asp:ListItem>
            <asp:ListItem>25</asp:ListItem>
            <asp:ListItem>30</asp:ListItem>
            <asp:ListItem>35</asp:ListItem>
            <asp:ListItem>40</asp:ListItem>
            <asp:ListItem>45</asp:ListItem>
            <asp:ListItem>50</asp:ListItem>
            <asp:ListItem>55</asp:ListItem>
        </asp:DropDownList>
        </div>





来自eventStartDate,eventStartHour和eventStartMinute(TextBoxID)



我一直在尝试这个后台代码:





comes "eventStartDate", "eventStartHour" and "eventStartMinute" (TextBoxID)

and I've been trying this background code:

string date = arrivalDate.Text;
 string hour = arrivalHour.Text;
 string min = arrivalMinute.Text;

 //Set datestring and the format
 string dateString = date + " " + hour + ":" + min;
 string[] format = new string[] { "d/M/yyyy h:mm", "d/M/yyyy h:mm:ss" };

 DateTime dateTime;
 System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
 DateTime.TryParseExact(dateString, "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);





字符串dateString是否正确它具有这种格式=14-01-2015 03:15,但当我执行DateTime.TryParseExact时,它变为默认日期=0001-01-01 12:00:00 AM



请帮助



the string dateString is correct it has with this format = "14-01-2015 03:15" but when I do the DateTime.TryParseExact it becomes a default date ="0001-01-01 12:00:00 AM"

Help please

推荐答案

我首先看看你在dateString中传递给TryParseExact的确切字符串,并检查它的格式手动。但是......无论你通过什么,它都会失败。

您没有使用包含会议记录的任何格式,但您确实将会议记录(和冒号)附加到您收集的日期 - 然后尝试使用没有分钟或冒号的格式解析它:

I'd start by looking at exactly what string you are passing to TryParseExact in dateString, and checking it's format manually. But...it will fail, whatever you pass.
You aren't using either of your formats that include minutes, but you do append the minutes (and a colon) to the date you collect - and then try to parse it using a format that has no minutes or colon:
DateTime.TryParseExact(dateString, "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);

所以...它会失败。但是你没有检查返回bool值,所以你无法判断它是否有效。当它失败时,它只会将dateTime保留为默认值,最小值。



小错别字[/ edit]

So...it will fail. But you don't check the return bool value, so you can't tell if it did or not. When it fails, it just leaves dateTime at the default, minimum, value.

[edit]Minor typos[/edit]


这篇关于如何将其转换为单个DateTime变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆