Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="widget-wrap">
  <div id="hash"></div>
  <h1>TIME PICKER</h1>
  <label>12 hour</label>
  <input type="text" id="demoA"/>
  <label>24 hour</label>
  <input type="text" id="demoB"/>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-time-picker-javascript-css/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) WRAPPER */
#tp-wrap * {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}
#tp-wrap *::selection {
  background: transparent;
}
#tp-wrap {
  width: 100vw; height: 100vh;
  position: fixed; top: 0; left: 0; z-index: 999;
  background: rgba(0, 0, 0, 0.7);
  opacity: 0; visibility: hidden;
  transition: opacity 0.3s;
}
#tp-wrap.show {
  opacity: 1; visibility: visible;
}

/* (B) BOX */
#tp-box {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 320px;
  display: flex; flex-wrap: wrap;
  border: 1px solid #000;
  background: #2d2d2d;
}

/* (C) HR/MIN/AM/PM */
.tp-cell {
  width: 33.3%; padding: 0 15px;
  text-align: center;
}
.tp-up, .tp-down {
  padding: 10px 0;
  color: #e01717;
  font-size: 32px; font-weight: 700;
  cursor: pointer;
}
.tp-val {
  width: 100%; padding: 10px 0;
  text-align: center; font-size: 22px;
  background: #fff;
}

/* (D) CLOSE & SET BUTTON */
#tp-close, #tp-set {
  width: 50%; padding: 15px 0; border: 0;
  font-size: 18px; font-weight: 700;
  color: #fff; cursor: pointer;
}
#tp-close { background: #951a1a; }
#tp-set { background: #1339a7; }

/* (E) 24-HOUR MODIFY */
#tp-wrap.tp-24 #tp-ap { display: none; }
#tp-wrap.tp-24 #tp-hr, #tp-wrap.tp-24 #tp-min { width: 50%; }

/* (X) CSS DOES NOT MATTER - FOR COSMETICS */
/* PAGE & BODY */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1544991185-13fe5d113fe3?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MjAzOTkxMA&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* WIDGET */
.widget-wrap {
  width: 500px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.5);
}
.widget-wrap label, .widget-wrap input {
  display: block;
  padding: 10px;
  width: 100%;
}

/* SVG */
#hash {
  width: 100px; height:100px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 640 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M496 224c-79.59 0-144 64.41-144 144s64.41 144 144 144 144-64.41 144-144-64.41-144-144-144zm64 150.29c0 5.34-4.37 9.71-9.71 9.71h-60.57c-5.34 0-9.71-4.37-9.71-9.71v-76.57c0-5.34 4.37-9.71 9.71-9.71h12.57c5.34 0 9.71 4.37 9.71 9.71V352h38.29c5.34 0 9.71 4.37 9.71 9.71v12.58zM496 192c5.4 0 10.72.33 16 .81V144c0-25.6-22.4-48-48-48h-80V48c0-25.6-22.4-48-48-48H176c-25.6 0-48 22.4-48 48v48H48c-25.6 0-48 22.4-48 48v80h395.12c28.6-20.09 63.35-32 100.88-32zM320 96H192V64h128v32zm6.82 224H208c-8.84 0-16-7.16-16-16v-48H0v144c0 25.6 22.4 48 48 48h291.43C327.1 423.96 320 396.82 320 368c0-16.66 2.48-32.72 6.82-48z" /></svg>');
  background-repeat: no-repeat;
  background-position: center;
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 30px;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                var tp = {
  // (A) INIT - GENERATE TIME PICKER HTML
  hwrap : null, // entire html time picker
  hhr : null,   // html hour value
  hmin : null,  // html min value
  hap : null,   // html am/pm value
  init : () => {
    // (A1) ADD TIME PICKER TO BODY
    tp.hwrap = document.createElement("div");
    tp.hwrap.id = "tp-wrap";
    document.body.appendChild(tp.hwrap);

    // (A2) TIME PICKER INNER HTML
    tp.hwrap.innerHTML =
    `<div id="tp-box">
      <div class="tp-cell" id="tp-hr">
        <div class="tp-up">&#65087;</div> <div class="tp-val">0</div> <div class="tp-down">&#65088;</div>
      </div>
      <div class="tp-cell" id="tp-min">
        <div class="tp-up">&#65087;</div> <div class="tp-val">0</div> <div class="tp-down">&#65088;</div>
      </div>
      <div class="tp-cell" id="tp-ap">
        <div class="tp-up">&#65087;</div> <div class="tp-val">AM</div> <div class="tp-down">&#65088;</div>
      </div>
      <button id="tp-close" onclick="tp.hwrap.classList.remove('show')">Close</button>
      <button id="tp-set" onclick="tp.set()">Set</button>
    </div>`;

    // (A3) GET VALUE ELEMENTS + SET CLICK ACTIONS
    for (let segment of ["hr", "min", "ap"]) {
      let up = tp.hwrap.querySelector(`#tp-${segment} .tp-up`),
          down = tp.hwrap.querySelector(`#tp-${segment} .tp-down`);
      tp["h"+segment] = tp.hwrap.querySelector(`#tp-${segment} .tp-val`);

      if (segment=="ap") {
        up.onclick = () => { tp.spin(true, segment); };
        down.onclick = () => { tp.spin(true, segment); };
      } else {
        up.onmousedown = () => { tp.spin(true, segment); };
        down.onmousedown = () => { tp.spin(false, segment); };
        up.onmouseup = () => { tp.spin(null); };
        down.onmouseup = () => { tp.spin(null); };
        up.onmouseleave = () => { tp.spin(null); };
        down.onmouseleave = () => { tp.spin(null); };
      }
    }
  },

  // (B) SPIN HOUR/MIN/AM/PM
  //  direction : true (up), false (down), null (stop)
  //  segment : "hr", "min", "ap" (am/pm)
  timer : null, // for "continous" time spin
  minhr : 1,    // min spin limit for hour
  maxhr : 12,   // max spin limit for hour
  minmin : 0,   // min spin limit for minute
  maxmin : 59,  // max spin limit for minute
  spin : (direction, segment) => {
    // (B1) CLEAR TIMER
    if (direction==null) { if (tp.timer!=null) {
      clearTimeout(tp.timer);
      tp.timer = null;
    }}

    // (B2) SPIN FOR AM/PM
    else if (segment=="ap") { tp.hap.innerHTML = tp.hap.innerHTML=="AM" ? "PM" : "AM"; }

    // (B3) SPIN FOR HR/MIN
    else {
      // (B3-1) INCREMENT/DECREMENT
      let next = +tp["h"+segment].innerHTML;
      next = direction ? next+1 : next-1;

      // (B3-2) MIN/MAX
      if (segment=="hr") {
        if (next > tp.maxhr) { next = tp.maxhr; }
        if (next < tp.minhr) { next = tp.minhr; }
      } else {
        if (next > tp.maxmin) { next = tp.maxmin; }
        if (next < tp.minmin) { next = tp.minmin; }
      }

      // (B3-3) SET VALUE
      if (next<10) { next = "0"+next; }
      tp["h"+segment].innerHTML = next;

      // (B3-4) KEEP ROLLING - LOWER TIMEOUT WILL SPIN FASTER
      tp.timer = setTimeout(() => { tp.spin(direction, segment); }, 100);
    }
  },

  // (C) ATTACH TIME PICKER TO HTML FIELD
  //  target : html field to attach to
  //  24 : 24 hours time? default false.
  //  after : optional, function to run after selecting time
  attach : (instance) => {
    // (C1) READONLY FIELD + NO AUTOCOMPLETE
    // IMPORTANT, PREVENTS ON-SCREEN KEYBOARD
    instance.target.readOnly = true;
    instance.target.setAttribute("autocomplete", "off");

    // (C2) DEFAULT 12 HOURS MODE
    if (instance["24"]==undefined) { instance["24"] = false; }

    // (C3) CLICK TO OPEN TIME PICKER
    instance.target.addEventListener("click", () => { tp.show(instance); });
  },

  // (D) SHOW TIME PICKER
  setfield : null, // set selected time to this html field
  set24 : false,   // 24 hours mode? default false.
  setafter : null, // run this after selecting time
  show : (instance) => {
    // (D1) INIT FIELDS TO SET + OPTIONS
    tp.setfield = instance.target;
    tp.setafter = instance.after;
    tp.set24 = instance["24"];
    tp.minhr = tp.set24 ? 0 : 1 ;
    tp.maxhr = tp.set24 ? 23 : 12 ;

    // (D2) READ CURRENT VALUE
    let val = tp.setfield.value;
    if (val=="") {
      tp.hhr.innerHTML = "0"+tp.minhr;
      tp.hmin.innerHTML = "0"+tp.minmin;
      tp.hap.innerHTML = "AM";
    } else {
      tp.hhr.innerHTML = val.substring(0, 2);
      if (tp.set24) {
        tp.hmin.innerHTML = val.substring(2, 4);
      } else {
        tp.hmin.innerHTML = val.substring(3, 5);
        tp.hap.innerHTML = val.substring(6, 8);
      }
    }

    // (D3) SHOW TIME PICKER
    if (tp.set24) { tp.hwrap.classList.add("tp-24"); }
    else { tp.hwrap.classList.remove("tp-24"); }
    tp.hwrap.classList.add("show");
  },

  // (E) SET TIME
  set : () => {
    // (E1) TIME TO FIELD
    if (tp.set24) {
      tp.setfield.value = tp.hhr.innerHTML + tp.hmin.innerHTML;
    } else {
      tp.setfield.value = tp.hhr.innerHTML + ":" + tp.hmin.innerHTML + " " + tp.hap.innerHTML;
    }
    tp.hwrap.classList.remove("show");

    // (E2) RUN AFTER, IF SET
    if (tp.setafter) { tp.setafter(tp.setfield.value); }
  }
};

// (F) ATTACH ON PAGE LOAD
document.addEventListener("DOMContentLoaded", () => {
  tp.init();
  tp.attach({
    target: document.getElementById("demoA")
  });
  tp.attach({
    target: document.getElementById("demoB"),
    "24" : true
  });
});

              
            
!
999px

Console