Search code examples
javascriptjqueryjquery-ui-datepicker

JQuery datepicker with a line break between day and month


I want to add a <br> or something in the input between the day and the month, using JQuery datepicker.

I have the code below:

jQuery('#checkin').datepicker({
        showAnim: "drop",
        dateFormat: "dd/mm/yy",
        minDate: "-0D",

    });

    jQuery('#checkout').datepicker({
        showAnim: "drop",
        dateFormat: "dd/MM/yy",
        minDate: "-0D",
        beforeShow: function () {
            var a = jQuery("#checkin").datepicker('getDate');
            if (a) return {
                minDate: a
            }
        }
    });

To be more specific the final input is like this:
enter image description here

and I want to have something like this:
enter image description here

Thanks a lot!


Solution

  • Using only the datepicker I think it's not possible. But you can use a bit of Javascript to do it ;-)

    Check this:

    $(document).ready(function() {
    
      $('#date').on('click', function() {
        $('#thedate').datepicker('show');
      });
    
      $('#thedate').datepicker({
        dateFormat: 'd M yy',
        onSelect: function(selectedDate) {
          var arr = selectedDate.split(" ");
          $('#date').html("<span class='date_day'>" + arr[0] + "</span><span class='date_month'>" + arr[1] + "</span> <span class='date_year'>" + arr[2] + "</span>");
        }
      });
    
    });
    #date {
      padding: 10px;
      border: solid 1px black;
      width: 100px;
      text-align: center;
      cursor: pointer;
      font-family: Arial,Helvetica Neue,Helvetica,sans-serif
    }
    
    #date span.date_day {
      font-weight: bold;
      display: block;
    }
    
    #date span.date_month,
    #date span.date_year {
      color: gray;
      font-size: 12px;
    }
    <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    
    <div id='date'>
      Select a date
    </div>
    <input id="thedate" type="hidden" /><br />