JQuery Datepicker: displaying one date format for the user, using a different date format in the background

I have input using datepicker tool from jQuery. Now I know that I can set the date format used by datepicker. The problem is this: I would like to show the date in German format for the user using the English format for the database (from dd-mm-yy to yy-mm-dd).

Now I have the following:

$(".date").datepicker({ dateFormat: "dd-mm-yy", onSelect: function(dateText, inst) { $("#test").html($.inst.formatDate('yy-mm-dd', dateText); // What exactly do I have to do, to get the date from dd-mm-yy to yy-mm-dd } }); 
+4
source share
2 answers

Probably your best option is to use the altField and altFormat for the datepicker, which is for this very reason: API Docs .

If you set altField to a hidden input field, you can get the data from there when loading into the database or for something else that you need in an alternative date format.

+6
source

I use this for the form field:

 echo 'Set next action date: <input type="text" id="next_action" name="next_action" value="' . $next_action . '" />'; 

In the scipt setting for datepicker, I enable this option:

 dateFormat: 'yy-mm-dd', 

This gives me the correct MySQL date format for inserting / updating in a table. But it is not very friendly to visitors, so I set my preferred format at the top of the page:

 if ($s['next_action'] >= "1") { $next_action = date($date_style,strtotime($s['next_action'])); } else { $next_action = ''; } 

It depends on the variable in my main include file:

 $date_style = "F d, Y"; 

If I select a date, datepicker gives me a good date for the database on submit, although it shows the format yyyy-mm-dd until it is sent. After sending it uses a nice display.

Van

+2
source

All Articles