views:

420

answers:

4

So I'm building an application where data is coming back from a database where the date could be in many different formats, ie '2008', '05/22/99', '8/20/2004'. And lets say, for example, a user wants to edit a date (2009) by clicking the "Edit" button. Well in the edit window it SHOULD show an input box with the year '2009' already present within the box. The problem is, however, datepicker sees 2009, doesn't recognize this date format as the default and instead plugs in today's date.

This is obviously a huge problem that will inevitably cause a crazy amount of confusion when somebody goes to edit the dates.

Is there a way to make this behavior stop? Am I missing something simple in my setup?

I'm sorry I don't have an example online and many thanks to anyone who has any ideas on how to overcome this.

Thanks,

Jeff

A: 

You should use JQuery to set the value of the textbox after the datepicker has been attached to the textbox.

$('txtBox').datepicker();
$('txtBox').val('2009');
or
document.getElementById('txtBox').value = '2009';

As long as you do it after the datepicker code has been attached, your preset value shouldn't get overwritten.

LesterDove
I thought the same thing but when I tried it didn't work. Alerting the current value for the textboxes returned "undefined" even though Firebug showed '2009'. Not sure why...
Jeff
+1  A: 

Normally it would not change it ..

The example at http://jqueryui.com/demos/datepicker/ does not do what you describe. So it might be one of the options you use, or an Event you have intercepted like onClose ..

Show us some code ..

Gaby
yeah the default behavior does not change the text-box value until the user explicitly selects a date. http://jsbin.com/aroca
Anurag
That example doesn't already have a value for the input textbox upon datepicker initialization.(Quick example)database generated html/table:<table><tr><th>Date:</th><th>Action</th></tr><tr><td>2009</td><td><a onclick="Edit(this)">EDIT</a></tr></table>Using jQuery:function Edit(obj){var tbody = $(obj).parent().find('td');var editTextBox = "<input class='jDate' type='text' value='" + $(tbody).find(':first').text() + "' />";}$('.jDate').datepicker();Now when editTextBox is drawn on another part of the page it's value will NOT be what I set it to but today's date.
Jeff
yikes. sorry for the formatting above...
Jeff
Nice link Anurag. That will come in hand :)But you're right. Works fine there:http://jsbin.com/ubapa3
Jeff
+1  A: 

Best case is to store dates in your database in the exact same format. Next best case, re-format the date either on the server or using javascript so that the dates given to the datepicker are all in the same format, making everyone's life easier.

Javascript has plenty of ways to manipulate dates.

carillonator
I completely disagree. Best case would be to store the dates however the user entered them. If the user wanted to store a date as 2009, who are we to say they are wrong and display a different date than what they entered. Obviously though this makes a programmer's life a bit more frustrating. But it's a necessity to good application user design.
Jeff
I would agree if a date were not a Completely Objective piece of data. Writing a date in different ways does absolutely nothing for its meaning, so in this case, good 'application user design' is to take inconsistencies and make them consistent. If you really want to deal with this, knock yourself out.
carillonator
A: 

Apparently this problem occurs after datepicker options are set after the original datepicker is initialized.

This link posted earlier works fine, but this does not and illustrates my problem: http://jsbin.com/axipe

A bug maybe?

Thanks for everyone's suggestions and comments!

Cheers,

Jeff

Jeff