Click here to Skip to main content
15,883,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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>


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...
Posted
Updated 23-Jan-15 5:12am
v2
Comments
V. 23-Jan-15 8:10am    
can you update the question and add the lines where you use the parsing functions?
jaket-cp 23-Jan-15 8:10am    
try
https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx
DateTime(Int32, Int32, Int32)
DateTime(Year, Month, Day)
varmartins 23-Jan-15 11:16am    
The date comes together (the day, month, and year) and then hour and minute, these 3 are separate things not sure any of those can help
jaket-cp 23-Jan-15 12:18pm    
oops missed the hour and minutes part.
Same link:
https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.

Link off to:
https://msdn.microsoft.com/en-us/library/wb249tb7(v=vs.110).aspx

In example:
Dim date1 As New Date(2010, 8, 18, 16, 32, 18, 500)

Hope that helps out.

opps that was vb.net example.
C# exmaple:
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 18, 500);
varmartins 23-Jan-15 13:08pm    
the problem is that my date is not separated like that

SELECT TIMEFROMPARTS ( 23, 59, 59, 0, 0 ) AS Result;
23:59:59
SELECT DATEFROMPARTS ( 2010, 12, 31 ) AS Result;
2010-12-31
 
Share this answer
 
Comments
jaket-cp 23-Jan-15 12:12pm    
nice 5ed
note only for MS SQL Server 2012+
varmartins 26-Jan-15 4:31am    
Yeah, I have MS SQL Server 2012
Hi,

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

C#
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
 
Share this answer
 
Comments
varmartins 23-Jan-15 11:10am    
I'm trying like this:

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 doesn't work not sure why but i put a breakpoint at the last point and the date is coming out something like 01-jan-01 or something like that, have to use tryparse or else it doesn't accept the values..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900