Jquery ui datepicker dateformat

I have some problems to get the correct date format.

After my date field has lost focus, I want it to display as follows:

dd-mm-yyyy, but it must be stored in a database, for example

yyyy-mm-dd ..

This is what I still have:

$("#birthdate").datepicker({ changeMonth: true, changeYear: true, altFormat: 'yyyy-mm-dd' }); 

Now it is shown in the date field as 11/25/2009 (November 25 09) and posted as 11/25/2009

any idea?

+4
source share
2 answers

Try the following:

 <script type="text/javascript" charset="utf-8"> $(document).ready(function() { $("#datepicker").datepicker({ altField: '#realdate', altFormat:'yy-mm-dd' }); }); 

 <input type="text" name="datepicker" value="" id="datepicker"> <input type="hidden" name="realdate" value="" id="realdate"> 

Then use realdate to get the date you post.

+4
source

MrHus has a good answer, but you should just process it for the backend, because this information can be faked through a proxy or in many other, possibly malicious ways. For example, if you use PHP, it will be something like this:

 <?php // BTW... you'll want to check for XSS attacks and other vulnerabilities first... $date = '08/05/1986';//$_REQUEST['datepicker']; // This is very simple... you should also check if the date is real... if(preg_match('/^\d{2}\/\d{2}\/\d{4}$/',$date)) { $date_formatted = date('Ym-d',strtotime($date)); } // put $date_formatted into database... ?> 

The combination of our two answers should provide you there (mostly) safely.

+2
source

All Articles