0

i have a calender control on my page which is generated using javascript. As shown below how it looks in my aspx page.

<td colspan="2"><asp:Label ID="lblPckgStartDate" runat="server" Text="Pckg Start Date"></asp:Label>
                <input ID="txtPckgStartDate" runat="server" class="NormalTextBox" ReadOnly=ReadOnly type="text" />&nbsp;<a onclick="showCalendarControl(txtPckgStartDate)" href="#"><img src="calendar.gif" style="width: 20px; height: 20px" border=0 /></a>
</td>

Now when i access this textbox value on server side and load it into string variable and then pass it to date time object it gives me following error.

DateTime date = new DateTime();
string dateinddmmyyyy = txtPckgStartDate.Value.Trim();
//date in mm/dd/yyyy format
date = Convert.ToDateTime(dateinddmmyyyy);

Error:
Failed to convert parameter value from a String to a DateTime.

Plz provide me some solutions to convert string to datetime ASAP.

Edited by __avd: For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
1
Reply
6
Views
7 Years
Discussion Span
Last Post by __avd
0

Use DateTime.TryParseExact method.

DateTime date;
        string[] format = { "dd/MM/yyyy", "dd-MM-yyyy" };
        string dateinddmmyyyy = "31/12/2003";

        DateTime.TryParseExact(dateinddmmyyyy, format, null, System.Globalization.DateTimeStyles.None, out date);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.