admin管理员组

文章数量:1390480

im using jQuery datepicker in my php code specifically for the browsers like IE and Firefox. The default date format of datepicker is mm/dd/yyyy, however for chrome, since the date picker is already available, it shows dd/mm/yyyy. Whatever the display format is, i only want to send back to the server 'yyyy-mm-dd' or as in php date('Y-m-d)

How can i display the date from datepicker as dd/mm/yyyy and send to database as yyy-mm-dd? In doing so, i don't want to affect Chrome which is already working properly. Thanks for any advice.

im using jQuery datepicker in my php code specifically for the browsers like IE and Firefox. The default date format of datepicker is mm/dd/yyyy, however for chrome, since the date picker is already available, it shows dd/mm/yyyy. Whatever the display format is, i only want to send back to the server 'yyyy-mm-dd' or as in php date('Y-m-d)

How can i display the date from datepicker as dd/mm/yyyy and send to database as yyy-mm-dd? In doing so, i don't want to affect Chrome which is already working properly. Thanks for any advice.

Share Improve this question asked Nov 27, 2015 at 9:42 schenkerschenker 9633 gold badges13 silver badges27 bronze badges 3
  • 1 could you post the code where you initialize the datepicker? – Imesh Chandrasiri Commented Nov 27, 2015 at 9:43
  • How are you "sending back to the server"? How are you "sending to the database"? If you're sending to server via javascript (as per tags) then you can parse it/reformat it before sending. You could also parse the date server-side before sending to the DB. – fdomn-m Commented Nov 27, 2015 at 9:44
  • How are you formatting your <input>? Chrome will (incorrectly IMO) draw a date box differently if it's <input type='date'> – fdomn-m Commented Nov 27, 2015 at 9:45
Add a ment  | 

4 Answers 4

Reset to default 4

You can use the dateFormat option:

$( "#datepicker" ).datepicker( 
  "option", "dateFormat", "yy-mm-dd"
);

Set your datepicker format try this :-

$("#datepicker").datepicker("option", "dateFormat", 'yy-mm-dd' );

More details

You have to specify dateFormat other wise its dependent to browser.

  $(function() {
    $( "#datepicker" ).datepicker();
    $( "#format" ).change(function() {
      $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
    });
  });
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Format date</title>
  <link rel="stylesheet" href="//code.jquery./ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery./jquery-1.10.2.js"></script>
  <script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">

</head>
<body>
 
<p>Date: <input type="text" id="datepicker" size="30"></p>
 
<p>Format options:<br>
  <select id="format">
    <option value="mm/dd/yy">Default - mm/dd/yy</option>
    <option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
    <option value="d M, y">Short - d M, y</option>
    <option value="d MM, y">Medium - d MM, y</option>
    <option value="DD, d MM, yy">Full - DD, d MM, yy</option>
    <option value="&apos;day&apos; d &apos;of&apos; MM &apos;in the year&apos; yy">With text - 'day' d 'of' MM 'in the year' yy</option>
  </select>
</p>
 
 
</body>
</html>

Reference: jqueryui datepicker

Thanks to Rahautos. I did this:

<script type="text/javascript">
if (datefield.type!="date"){     jQuery(function($){         
    $('#datepicker').datepicker(); 
    $("#datepicker").datepicker("option", "dateFormat", 'yy-mm-dd' );        
})

}
</script>

本文标签: javascriptjquery datepicker has different format on different browsersStack Overflow