Advertisement
Ultroman

Monthpicker instantiation - First of the month

Oct 12th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This monthpicker requires my monthpicker.js and monthpicker.css available here: https://pastebin.com/u/Ultroman
  2. // If you want to see the instructions, go to this StackOverflow post: https://stackoverflow.com/a/26011321/1289974
  3.  
  4. // The value retrieved from this monthpicker, is always the FIRST day of the selected month.
  5. // Starts at the current month, and ranges from 100 years ago and 10 years into the future.
  6.  
  7.         $('#MyMonthTextBox').monthpicker({
  8.             dateFormat: 'MM yy',
  9.             changeMonth: true,
  10.             changeYear: true,
  11.             showMonthAfterYear: true,
  12.             showAnim: "drop",
  13.             constrainInput: true,
  14.             yearRange: "-100Y:+10Y",
  15.             minDate: new Date(new Date().getFullYear() - 100, new Date().getMonth(), 1),
  16.             maxDate: new Date((new Date().getFullYear() + 10), new Date().getMonth(), 1),
  17.             defaultDate: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
  18.            
  19.             // Monthpicker functions
  20.             onClose: function (dateText, inst) {
  21.                 var date = new Date(inst.selectedYear, inst.selectedMonth, 1);
  22.                 $(this).monthpicker('option', 'defaultDate', date);
  23.                 $(this).monthpicker('setDate', date);
  24.             },
  25.  
  26.             beforeShow: function (input, inst) {
  27.                 if ($(this).monthpicker("getDate") !== null) {
  28.                     // Making sure that the date set is the first of the month.
  29.                     if($(this).monthpicker("getDate").getDate() !== 1){
  30.                         var date = new Date(inst.selectedYear, inst.selectedMonth, 1);
  31.                         $(this).monthpicker('option', 'defaultDate', date);
  32.                         $(this).monthpicker('setDate', date);
  33.                     }
  34.                 } else {
  35.                     // If the date is null, we reset it to the defaultDate. Make sure that the defaultDate is always set to the first of the month!
  36.                     $(this).monthpicker('setDate', $(this).monthpicker('option', 'defaultDate'));
  37.                 }
  38.             },
  39.             // Special monthpicker function!
  40.             onChangeMonthYear: function (year, month, inst) {
  41.                 $(this).val($.monthpicker.formatDate($(this).monthpicker('option', 'dateFormat'), new Date(year, month - 1, 1)));
  42.             }
  43.         })
  44.         //.after( // this makes a link labeled "clear" appear to the right of the input-field, which clears the text in it
  45.         //    $("<a href='javascript: void(0);'>clear</a>").click(function() {
  46.         //        $(this).prev().val('');
  47.         //    })
  48.         //)
  49.         ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement