views:

55

answers:

4

I'm working on an application where dates are passed as yyyy-mm-dd but displayed to the client as mm-dd-yyyy (US based client).

I'm using a jQuery datepicker for selecting from/to dates in a report and would like to show the dates to the client as mm-dd-yyyy but when the form is submitted, submit as yyyy-mm-dd.

Is it possible to display one value in a textinput textbox and have another submitted without needing to hook into onSubmit?

I've come up with the following alternatives:

  1. Hide the text input for the from and to fields, create dummy fields and make use of the following jQuery date picker functions: altField and altFormat to display the value to the client in their preferred way and deal only with the submitted values which are set through the alt functions.

  2. Have an onSubmit javascript call to change the dates from mm-dd-yyyy to dd-mm-yyyy and also change the value=" to change the date to the client's preferred format.

  3. Rewrite the app to handle all dates in mm-dd-yyyy and hope the client never has non-US customers that would like their dates in a specific format.

  4. Change all dates to dd-Mon-YYYY e.g. 26-May-2010 as all of our client's customers are guaranteed to be English only.

A: 

The way I do this is to translate before display and after submit in my PHP code. I am getting dates from a MySQL database which are in the 'YYYY-MM-DD' format, but displaying them in the form with a jQuery UI datepicker in the 'MM/DD/YYYY' format.

Sonny
This is the option I'm leaning towards.
Ivan Kruchkoff
It also has the side-effect of sanitizing the data.
Sonny
+3  A: 

Edit: thought you were wanting to use an additional field, how's this for using the same one?

http://jsfiddle.net/bEd8c/

format the date you want visible with dateFormat, use $.datepicker.formatDate() to send it the way you want. More specifically, on the form submit use it to re-format any datepickers.

$('form').submit(function() {
   $(this).find('input.yourDatePickerClass').each( function() {
      $dp = $(this);
      $dp.val( $.datepicker.formatDate( 'yy-mm-dd', $dp.datepicker('getDate')) );
   });
});

Old answer:

Take a look at the altFormat option in the datepicker. It lets you display a date format different to the one you're using.

e.g.

$( ".selector" ).datepicker({ altFormat: 'mm-dd-yy' });
Dan Heberden
In an alternate field, not in the field you're working with. My understanding and testing of it leads me to believe I need to introduce another field which will hold the date in an alternate format.
Ivan Kruchkoff
Ah - i thought you _wanted_ the dummy field stuff. I adjusted my answer accordingly.
Dan Heberden
I tend to stay away from JavaScript solutions for things like this because they can be easily circumvented by the end-user - intentionally or not.
Sonny
Well in the case of mm/dd/yy the user can ALWAYS circumvent it.. i mean, if they enter 05/06/2010 how can you check if that's mm/dd or dd/mm? Even if you handled the string originally, they could still reverse it, ya know? Though sending the whole date string in it's original format from the date picker, i think, would be best to handle on the server (like you said, rely on server processing - i agree with you there. Didn't seem like a valid avenue re: the OP question)
Dan Heberden
I like this approach, although it is effectively hooking into the onsubmit, I've bookmarked it as I do see it coming in handy in future, cheers.
Ivan Kruchkoff
A: 

altField+altFormat is exactly what you want.

Matt Ball
my form is currently like this<form><input name="date_from" id="datepicker_from" type="text" />...</form>If I use altField + altFormat, I need to hide the text input, add a new textbox and use the two methods to display the date to the user in the new form elements while always working with the date in the hidden elements...or am I missing something?
Ivan Kruchkoff
Sounds about right to me.
Matt Ball
I don't want the extra form fields.
Ivan Kruchkoff
+1  A: 

You should always sanitize and format your dates before passing them to the DB. This isn't something that should be handle on the client side. A simple date("Y-m-d", strtotime($date)) should suffice.

Kenaniah
The site is private login, the client won't be trying to maliciously alter form inputs and all database calls are parameterised.
Ivan Kruchkoff
Even so, you should *never* trust your users. Sanitization and validation should always be a focus when dealing with data. Good to hear that you use parameters for your queries :-)
Kenaniah