如何将由不同字符串组成的日期保存到DB? [英] How do I save a date made up of different strings to DB?

查看:62
本文介绍了如何将由不同字符串组成的日期保存到DB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我有这个代码:



Hey guys, I have this code:

<div>
            <p>
                2.6. Arrival date and time (*):
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="arrivalDate" ForeColor="red" ErrorMessage="<- Required"></asp:RequiredFieldValidator>
            </p>
            <asp:TextBox ID="arrivalDate" runat="server" Width="100px"></asp:TextBox>
            <ajaxToolkit:CalendarExtender ID="arrivalDate_CalendarExtender" OnClientDateSelectionChanged="dates"
                Format="dd-MM-yyyy" TargetControlID="arrivalDate"  runat="server">
            </ajaxToolkit:CalendarExtender>
            at
        <asp:DropDownList ID="arrivalHour" 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="arrivalMinute" 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>





从这段代码我可以获得3个值(字符串),arrivalDate.Text,arrivalHour .Text和arrivalMinute.Text ...我想要的是能够将它们变成DateTime类型的1个变量,以便能够保存具有该类型的DB并正确格式化...我正在尝试使用DateTime.ParseExact ()和DateTime.Parse似乎没有任何工作......请帮忙!





我正在尝试这段代码:< br $>




and from this code i can get 3 values(strings), arrivalDate.Text , arrivalHour.Text and arrivalMinute.Text ... what I want is to be able to get them into 1 variable of the type DateTime to be able to save the DB with that type and correctly formated... I've trying with DateTime.ParseExact() and DateTime.Parse but nothing seems to be working... help please!


I'm trying this code :

string date = arrivalDate.Text; //arrivalDate.Text
            int hour; 
            Int32.TryParse(arrivalHour.Text, out hour);
            string min = arrivalMinute.Text; //arrivalMinute.Text

            //Set your datestring and the format
            string dateString = date + " " + hour + ":" + min;
            string format = "dd/MM/yyyy hh:mm";

            CultureInfo culture = new CultureInfo("fr-FR");

            DateTime dateTime;
            DateTime.TryParseExact(dateString, format, culture, DateTimeStyles.None, out dateTime);
            Console.WriteLine(dateTime);





但它不起作用...



but it's not working...

推荐答案

SELECT TIMEFROMPARTS(23,59,59,0,0)AS结果;

23:59:59

SELECT DATEFROMPARTS(2010, 12,31)AS结果;

2010-12-31
SELECT TIMEFROMPARTS ( 23, 59, 59, 0, 0 ) AS Result;
23:59:59
SELECT DATEFROMPARTS ( 2010, 12, 31 ) AS Result;
2010-12-31






我目前已经对它们进行了硬编码,你可以替换确切的值。



Hi,

I have currently hardcoded them, you can replace the exact values.

string date = "01/01/2014"; //arrivalDate.Text
int hour = Convert.ToInt32("15"); //arrivalHour.Text
string min = "12"; //arrivalMinute.Text

//Do some processing
string type = hour > 12 ? "PM" : "AM";
hour = hour > 12 ? hour - 12 : hour;

//Set your datestring and the format
string dateString = date + " " + hour + ":" + min + " " + type;
string format = "dd/MM/yyyy h:mm tt";

DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Console.WriteLine(dateTime);





Srikant



Srikant


这篇关于如何将由不同字符串组成的日期保存到DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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