Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have datetime input textboxes for StartDate and EndDate by using asp:TextBox with ajaxtoolkit maskeditextender. Enddate is as same as StartDate code. Here is the code.

ASP.NET
<asp:TextBox ID="txtStartDateTime" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender1"
    TargetControlID="txtStartDateTime"  runat="server" Mask="99/99/9999 99:99"
    MessageValidatorTip="true" MaskType="DateTime"  ClearMaskOnLostFocus="true"
    ClearTextOnInvalid="true" AcceptAMPM="true" UserDateFormat="DayMonthYear"
    ErrorTooltipEnabled="True">
</ajaxToolkit:MaskedEditExtender>

<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator1"  runat="server" 
    ControlToValidate="txtStartDateTime" ControlExtender="MaskedEditExtender1"
    IsValidEmpty="false" EmptyValueMessage="Input Date and Time" 
    InvalidValueMessage="Date Time Input Not Valid">
</ajaxToolkit:MaskedEditValidator>


This is C# code behind to get date and time of userinput and save in database.
C#
string strStartDateTime = txtStartDateTime.Text;
           DateTime dtStartingDateTime = new DateTime();
           dtStartingDateTime = DateTime.ParseExact(strStartDateTime, "dd/MM/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);


I am not passing at c# code. It shows error like this.


System.FormatException: String was not recognized as a valid DateTime.

at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)

at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)


Can anyone help me how to fix. I googled the problem and no solution. Thank in advance.
Posted

1 solution

Check that strStartDateTime actually has the format "09/07/2014 14:28"
If not you have to change your format string in DateTime.ParseExact so it matches your input string.

PS. The title of your question should actually be "How to convert date as a string to DateTime?"
 
Share this answer
 
v2

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