Datepicker - Separate YY and MM Only

I am trying to have two separate fields for YY and MM. What I have now is more or less what I want, except for only one field. Here's what it looks like now: how it looks now

 <input id="residenceyears" name="residenceyears" type="text"    placeholder="YY/MM" class="input yyMM numeric-only">

This is the JS function:

<script type="text/javascript">
$('.yyMM').datepicker({
 changeMonth: true,
 changeYear: true,
 monthNames: ["1","2","3","4","5","6","7","8","9","10","11","12"],
 yearRange: "-100:+0", 
 dateFormat: 'yy/mm',

 onClose: function() {
    var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
    var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
    $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
 },

 beforeShow: function() {
   if ((selDate = $(this).val()).length > 0) 
   {
      iYear = selDate.substring(selDate.length - 4, selDate.length);
      iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5), $(this).datepicker('option', 'monthNames'));
      $(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
       $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
   }
}

});

I'm trying to achieve this: it should look like this

+4
source share
2 answers

Take a look at this jsFiddle -> https://jsfiddle.net/mLcpwdra/

The code:

HTML

<div id="datepicker"></div>
<hr />
<div id="month"><p>Month:<span></span></p></div>
<div id="year"><p>Year:<span></span></p></div>
<div id="day"><p>Day:<span></span></p></div>

Js

var obj = {
    init: function()
  {
    $('#datepicker').datepicker({
        onSelect : function(date_string,instance)
      {
        var date_obj = new Date(date_string);
        $('#month span').text(String("00"+date_obj.getMonth()).slice(-2));
        $('#year span').text(date_obj.getFullYear());
        $('#day span').text(String("00"+date_obj.getDate()).slice(-2));
      }
    });
  }
};

$(document).ready(function(){
    obj.init();
});

This is a starting point, but I think it suits your needs. Hope this helps

UPDATE : script with a year in the format YY "-> https://jsfiddle.net/mLcpwdra/1/

0

, .

HTML:

<select class="form-control" id="yearMonthInput"></select>

javascript:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    for (i = new Date().getFullYear() ; i > 2008; i--) {
        $.each(months, function (index, value) {
            $('#yearMonthInput').append($('<option />').val(index + "_" + i).html(i + " " + value));
        });                
    }

! , .

0

Source: https://habr.com/ru/post/1648616/


All Articles