0

I needed a date picker to add into a PHP file I use to select various time ranges inside one of my database tables. I used to type this information manually but because I need to select it so often I wanted to create a faster cleaner version. I found source code for a datepicker through this website..

http://api.jqueryui.com/datepicker/#option-dateFormat

It gives me the following code to use...

    <script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );
    $( function() {
    $( "#datepicker2" ).datepicker();
  } );
  </script>

One is for the start date and the second is for the finish date, I'm trying to modify the format of the dates so they'll appear yy-mm-dd. The API documentation says I can accomplish this by using the following code...

    $( ".selector" ).datepicker({
  dateFormat: "yy-mm-dd"
});

    // Getter
var dateFormat = $( ".selector" ).datepicker( "option", "dateFormat" );

// Setter
$( ".selector" ).datepicker( "option", "dateFormat", "yy-mm-dd" );

How do I add this to my code? Does is just go right inside the script tags? I tried putting it there and making various edits but with no success, the API docs don't detail where to add it or what to modify.

The entire source code chunk that powers this is..

 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );
    $( function() {
    $( "#datepicker2" ).datepicker();
  } );
  $( ".datepicker" ).datepicker({
  dateFormat: "yy-mm-dd"
});
$( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
  </script>
 <form action="dateselection.php" method="POST">
<p>Date: <input type="text" id="datepicker" name="dp"></p>
<p>Date2: <input type="text" id="datepicker2" name="dp2"></p>
 <input type="submit">
 </form>

1 Answer 1

1

You can simply declare the dateFormat in the initial datepicker() declaration, such as:

<form action="dateselection.php" method="POST" id="datePost">
    <p>Date: <input type="text" id="datepicker" name="dp"></p>
    <p>Date2: <input type="text" id="datepicker2" name="dp2"></p>
    <input type="submit" value="Submit">
</form>
<script>
    $("#datepicker").datepicker({ dateFormat: "yy-mm-dd" });
    $("#datepicker2").datepicker({ dateFormat: "yy-mm-dd" });
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.