cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <!DOCTYPE html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>A Date Chooser</title>
  <script src="dateui.js"></script>
  <script src="datepicker.js"></script>
</head>
<body>
  <h2>A Date Chooser</h2>
  <form id="theForm">
    Year: <input type="text" name="year" id="year" size="4" maxLength="4">
    Month: <select name="month" id="month">
      <option id="month1" value="1">Jan</option>
      <option id="month2" value="2">Feb</option>
      <option id="month3" value="3">Mar</option>
      <option id="month4" value="4">Apr</option>
      <option id="month5" value="5">May</option>
      <option id="month6" value="6">Jun</option>
      <option id="month7" value="7">Jul</option>
      <option id="month8" value="8">Aug</option>
      <option id="month9" value="9">Sep</option>
      <option id="month10" value="10">Oct</option>
      <option id="month11" value="11">Nov</option>
      <option id="month12" value="12">Dec</option>
    </select>
    Day: <select name="day" id="day"></select>&nbsp;
    <input type="text" name="dayInWeek" id="dayInWeek" readonly>&nbsp;
    <input type="button" id="btnNow" value="Now">
  </form>
</body>
</html>
            
          
!
            
              // DateUtil.js
// Return true if the given year is a leap year
function isLeapYear(year) {
   return ((year % 4) === 0 && ((year % 100) !== 0 || (year % 400) === 0));
}
 
// Return the number of days in the given month (1-12) of the year (xxxx)
function getDaysInMonth(year, month) {
   if (month === 2) {
      if (isLeapYear(year)) {
         return 29;
      } else {
         return 28;
      }
   } else if ((month === 1) || (month === 3) || (month === 5) || (month === 7)
       || (month === 8) || (month === 10) || (month === 12)) {
      return 31;
   } else {
      return 30;
   }
}
 
// Get the day of the week given year, month (1-12) and day (1-31)
function getDayInWeek(year, month, day) {
  var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday",
                  "Thursday", "Friday", "Saturday"];
  var theDate = new Date(year, month-1, day);
  return weekdays[theDate.getDay()];
}

// JSDatePicker.js
window.onload = init;
 
// Global variables for the currently selected year, month and day
var selectedYear;   // 4-digit year
var selectedMonth;  // 1 to 12 for Jan to Dec
var selectedDay;    // 1 to 31
 
// The "onload" handler, runs after the page is fully loaded.
function init() {
   setToday();      // Set global variables
   updateDisplay();
 
   document.getElementById("year").onchange = function() {
      selectedYear=this.value;
      // In case the current day is no longer valid for non-leap year,
      // e.g., 29 Feb 2001. Set to the last year of the month
      updateDayDisplay();
      updateDayInWeekDisplay();
   }
 
   document.getElementById("month").onchange = function() {
      selectedMonth=this.value;
      // In case the current day is no longer valid, e.g., 31 in Feb.
      // Set to the last year of the month
      updateDayDisplay();
      updateDayInWeekDisplay()
   }
 
   document.getElementById("day").onchange = function() {
      selectedDay=this.value;
      updateDayInWeekDisplay();
   }
 
   document.getElementById("btnNow").onclick = function() {
      setToday();
      updateDisplay();
   }
}
 
// Set global variable selectedYear, selectedMonth and selectedDay
// to today.
function setToday() {
   var now = new Date();
   selectedYear = now.getFullYear();   // 4-digit year
   selectedMonth = now.getMonth() + 1; // 1 to 12 for Jan to Dec
   selectedDay = now.getDate();        // 1 to 31
}
 
// Update the year, month, day and day-in-week display according
// to the selected values.
function updateDisplay() {
   // Set the value of text fields and select the correct options
   document.getElementById("year").value = selectedYear;
   updateMonthDisplay();
   updateDayDisplay();
   updateDayInWeekDisplay();
}
 
function updateMonthDisplay() {
   document.getElementById("month" + selectedMonth).selected = true;
}
 
function updateDayDisplay() {
   var elm = document.getElementById("day");
   elm.innerHTML = "";
   var daysInMonth = getDaysInMonth(selectedYear, selectedMonth);
   // The selectedDay is no longer valid. Set to the last day of month
   if (selectedDay > daysInMonth) {
      selectedDay = daysInMonth;
   }
   var options = "";
   for (var day = 1; day <= daysInMonth; day++) {
      options += "<option value='" + day + "'";
      if (day === selectedDay) {
         options += " selected";
      }
      options += ">" + day + "</option>";
   }
   elm.innerHTML = options;
}
 
function updateDayInWeekDisplay() {
   document.getElementById("dayInWeek").value
      = getDayInWeek(selectedYear, selectedMonth, selectedDay);
}
            
          
!
999px
Loading ..................

Console