views:

349

answers:

1

I'm using GM Date Picker.. My code is as follows:

<cc1:GMDatePicker ID="DatePicker" AutoPosition="false" runat="server" CalendarTheme="Blue"
Style="z-index: 252; left: 0px; position: absolute; top: 0px" DateFormat="dd/MM/yyyy"
TodayButtonText="">

I've given the Date Format as mentioned above. But, it not taking in that format. It is taking only in this format MM/dd/YYYY. When I'm saving, it is taking the current date.. If i give in MM/dd/YYYY format, it is taking the correct date value..

How to overcome this problem?

+1  A: 

this control has a bug in the Date property get method, whenever date is read from the textbox the date format value is not taken into account. Here's an exact line of the Date property get method which throws an exception:

 DateTime time = DateTime.Parse(this.dateTextBox.Text, this.Culture);

the reason why you're getting a current date is that control caches all exceptions are returns current date in case one is occurred.

So what do you do, besides finding another control or asking vendor to fix this one. The workaround would be to get the date directly from control's textbox without using its Date property via reflection and parse it. Below is an example of how you can do this:

TextBox textBox = (TextBox)DatePicker.GetType().InvokeMember("dateTextBox",
    BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
    null, DatePicker, null);
if (textBox != null)
{
    DateTimeFormatInfo format = (new CultureInfo(DatePicker.Culture.Name)).DateTimeFormat;
    format.ShortDatePattern = DatePicker.DateFormat;
    DateTime date = DateTime.Parse(textBox.Text, format);
    Console.WriteLine(date.ToString());
}

hope this helps, regards

serge_gubenko
Hi! Thanks for your response.. When I'm debugging, it is getting the value correctly. But, at the last line when parsing, as usual it is taking in the format MM/dd/YYYY again. "DateTime date = DateTime.Parse(textBox.Text, format)"Wat to do?
Nila
Ok.. It is working. Thank you!!!!
Nila