Advertisement
Ultroman

Monthpicker instantiation - Last of the month

Oct 12th, 2014
1,143
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 LAST day of the selected month.
  5.     // Starts at the current month. Ranges from 100 years in the past to 10 years into the future.
  6.         $('#end').monthpicker({
  7.             dateFormat: 'MM yy',
  8.             changeMonth: true,
  9.             changeYear: true,
  10.             showMonthAfterYear: true,
  11.             showAnim: "drop",
  12.             constrainInput: true,
  13.             yearRange: "-100Y:+10Y",
  14.             minDate: new Date(new Date().getFullYear() - 100, new Date().getMonth() + 1, 0),
  15.             maxDate: new Date((new Date().getFullYear() + 10), new Date().getMonth() + 1, 0),
  16.             defaultDate: new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0),
  17.  
  18.             onClose: function (dateText, inst) {
  19.                 var date = new Date(inst.selectedYear, inst.selectedMonth + 1, 0);
  20.                 $(this).monthpicker('option', 'defaultDate', date);
  21.                 $(this).monthpicker('setDate', date);
  22.             },
  23.  
  24.             beforeShow: function (input, inst) {
  25.                 if ($(this).monthpicker("getDate") !== null) {
  26.                     // Making sure that the date set is the last of the month.
  27.                     var date = new Date(inst.selectedYear, inst.selectedMonth + 1, 0);
  28.                     if($(this).monthpicker("getDate").getDate() !== date){
  29.                         $(this).monthpicker('option', 'defaultDate', date);
  30.                         $(this).monthpicker('setDate', date);
  31.                     }
  32.                 } else {
  33.                     // If the date is null, we reset it to the defaultDate. Make sure that the defaultDate is always set to the last of the month!
  34.                     $(this).monthpicker('setDate', $(this).monthpicker('option', 'defaultDate'));
  35.                 }
  36.             },
  37.  
  38.             onChangeMonthYear: function (year, month, inst) {
  39.                 $(this).val($.monthpicker.formatDate($(this).monthpicker('option', 'dateFormat'), new Date(year, month, 0)));
  40.             }
  41.         })
  42.         //.after( // this makes a link labeled "clear" appear to the right of the input-field, which clears the text in it
  43.         //    $("<a href='javascript: void(0);'>clear</a>").click(function() {
  44.         //        $(this).prev().val('');
  45.         //    })
  46.         //)
  47.         ;
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement