views:

1435

answers:

2

Hello,

I gote some problems to get the dateformat right.

After my date field has lost focus I want it to show like:

dd-mm-yyyy but it has to be saved in the database like

yyyy-mm-dd. .

This is what i have so far:

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

Now it's shown in the date field like 11/25/2009 (nov 25th 09) and posted like 11/25/2009

any idea's?

+3  A: 

Try this:

<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 the realdate to get the date when you post.

MrHus
+1  A: 

MrHus has a good answer but you should just handle it on the backend because this information can be forged via a proxy or many other possibly malicious ways. If you are running PHP, for instance, it would 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('Y-m-d',strtotime($date));
}
// put $date_formatted into database...
?>

A combination of our two answers should get you there (mostly) safely.

KyleFarris