RMR
RMR

Reputation: 629

String to Datetime Conversion in Ajax CalendarExtender.

In Calender Control : when I select Date, it is display in TextBox in this format : 15-06-2015 00:00:00 means "dd-mm-yyyy hh:mm:ss".

After that I converted TextBox text into DateTime datatype using

DateTime dt = Convert.ToDateTime(txtDate.Text);

and store into database.

In database it store in this format : 2015-06-15 means "yyyy-mm-dd" because column datatype is Date.

In Ajax CalendarExtender : Now I am using CalendarExtender from AjexControlToolkit. I don't use format attribute. By default it display in textbox in this format : 6/16/2015 menas "mm/dd/yyyy"

try to convert Textbox string to datetime datatype using same code

DateTime dt = Convert.ToDateTime(txtDate.Text);

but error is displayed that "String was not recognized as a valid DateTime"

Even I also try

DateTime dt = DateTime.Parse(txtDate.Text);

but same error is displayed every time.

Upvotes: 1

Views: 1941

Answers (1)

Darren
Darren

Reputation: 70786

You have two options, provide the Format attribute for the AJAX control or use DateTime.ParseExact.

<ajaxToolkit:Calendar runat="server"
    TargetControlID="Date1"
    CssClass="ClassName"
    Format="yyyy-MM-dd"
    PopupButtonID="Image1" />

http://www.ajaxcontroltoolkit.com/Calendar/Calendar.aspx

Using ParseExact:

DateTime date = DateTime.ParseExact("6/16/2015", "M/dd/yyyy", null);

Upvotes: 0

Related Questions