Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                <header>
    <h1>Calculadora de horarios para tomar medicinas</h1>
  </header>
  <main>
    <form id="opciones" action="index.html" method="post" onsubmit="return false">
      <h3>Selecciona cada cuánto debes tomar tu medicina</h3>
      <fieldset id="standard">
        <legend>Estándar</legend>

        <label for="6horas">
          <input type="radio" name="freq" value="6" id="6horas">
          <span>6 horas</span>
        </label>
        <label for="8horas">
          <input type="radio" name="freq" value="8" id="8horas">
          <span>8 horas</span>
        </label>
        <label for="12horas">
          <input type="radio" name="freq" value="12" id="12horas">
          <span>12 horas</span>
        </label>
      </fieldset>
      <fieldset>
        <legend>Especiales</legend>
        <label for="3horas">
          <input type="radio" name="freq" value="3" id="3horas">
          <span>3 horas</span>
        </label>
        <label for="4horas">
          <input type="radio" name="freq" value="4" id="4horas">
          <span>4 horas</span>
        </label>
      </fieldset>
      <hr>
      <h3>Ingresa la hora de la primera dósis</h3>
      <input type="time" id="hora" name="hora" value="" pattern="[0-9]{2}:[0-9]{2}">
      <br>
      <label for="hora"><em>(Hora actual por defecto)</em></label>

      <div class="hidden"><hr>
      <h3 id="titTimeStandard">Cambia entre 24 horas y a.m./p.m.</h3>
      <fieldset id="timeStandard">
        <legend>Estándar de hora</legend>
        <label for="ampm">
          <input type="radio" name="am24" value="ampm" id="ampm" checked>
          <span>a.m./p.m.</span>
        </label>
        <label for="t24">
          <input type="radio" name="am24" value="t24" id="t24">
          <span>24 horas</span>
        </label>
      </fieldset></div>


    </form>

    <div id="result" class="hidden">
      <h3>Debes tomar tu medicina en estos horarios:</h3>
      <ul id="tomas">
      </ul>
      <ul id="sugerencia">
      </ul>
    </div>

  </main>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Roboto:ital,[email protected],400;0,700;1,400;1,700&display=swap');

:root {
  --accent: #06C1B3;
  --accent-shade: #179285;
}

body {
  background: rgb(2, 0, 36);
  background: -moz-linear-gradient(27deg, rgba(2, 0, 36, 1) 0%, rgba(9, 9, 121, 1) 92%);
  background: -webkit-linear-gradient(27deg, rgba(2, 0, 36, 1) 0%, rgba(9, 9, 121, 1) 92%);
  background: linear-gradient(27deg, rgba(2, 0, 36, 1) 0%, rgba(9, 9, 121, 1) 92%);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#020024", endColorstr="#090979", GradientType=1);
  color: #fff;
  font-family: 'Roboto', sans-serif;
  min-height: 100vh;
  font-size: 1.5rem;
  margin: 8px 35px;
}

header {
  max-width: 600px;
  margin: 20px auto 20px;
}

main {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: space-around;
  align-items: flex-start;
  max-width: 900px;
  margin: 0 auto;
  column-gap: 20px;
}

form#opciones {
  max-width: 441px;
  margin-bottom: 30px;
}

li.especial {
  color: var(--accent-shade);
  font-style: italic;
}

.hidden {
  display: none;
}

#timeStandard.unhide {
  display: flex;
}

hr {
  border: solid 1px var(--accent-shade);
  margin: 30px;
}

#sugerencia hr {
  width: 70%;
  display: block;
  margin: 10px 0 20px;
}

#result {
  max-width: 345px;
  border: 2px solid #fff;
  box-sizing: border-box;
  border-radius: 20px;
  padding: 5px 22px;
  align-self: center;
  background-color: #fff;
  color: rgb(2, 0, 36);
}

fieldset {
  border-radius: 20px;
  border: 2px solid #fff;
  margin-bottom: 14px;
  padding: 15px 10px 25px;
  color: var(--accent);
  display: flex;
  justify-content: space-evenly;
}

input[type="radio"] {
  display: none;
}

label span {
  align-items: center;
  padding: 0.375em 0.75em 0.375em 0.775em;
  border-radius: 99em;
  background-color: #fff;
  color: rgb(2, 0, 36);
  cursor: pointer;
}

label input:checked+span {
  background-color: var(--accent);
}

li {
  list-style: none;
  padding-bottom: 10px;
}

h3#titSugerencia {
  color: var(--accent-shade);
}

ul#sugerencia {
    color: var(--accent-shade);
    display: flex;
    flex-direction: column;
    align-items: center;
}

#result li {
    display: grid;
    grid-template-columns: 2rem 0.5rem 2rem 2.5rem;
    justify-content: center;
}
#result ul{
  padding: 0 2rem 0 0;
}

#result ul li span:nth-child(1){
  text-align: right;
}
#result ul li span:nth-child(2){
  text-align: center;
}
#result ul li span:nth-child(3){
  text-align: left;
}
#result ul li span:nth-child(4){
  text-align: left;
}

input#hora {
  font-size: 1.5rem;
  border-radius: 20px;
  padding: 4px 5px 4px 10px;
  border: none;
  margin-bottom: 5px;
}

@media screen and (max-width: 480px) {
  #standard span {
    font-size: 1rem;
    padding: .5rem;
  }
  #timeStandard span {
    font-size: 1.2rem;
    padding: .5rem;
  }
}

h3 {
  margin-bottom: 5px;
}

              
            
!

JS

              
                let dateNow = new Date();

const extractTime = (date) => {
  let exHour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  let exMinute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  return [exHour, exMinute]
}

let currentTime = extractTime(dateNow);

const inputs = document.querySelectorAll('#opciones input');



document.querySelector("#hora").value = currentTime[0] + ":" + currentTime[0];

const hoursHandler = () => {
  if (document.querySelector('#opciones input[type="radio"]:checked') == null) {
    return
  } else {
    let selection = document.querySelector('#opciones input[type="radio"][name="freq"]:checked').value;
    let t24 = document.querySelector('#timeStandard input[type="radio"]:checked').value == "t24";
    let tomas = 24 / selection;
    let selectedTime = document.querySelector('#hora').value;
    const list = document.getElementById('tomas');

    list.innerHTML = '';
    document.querySelector('#sugerencia').innerHTML = '';
    if (document.querySelector('#titSugerencia')) {
      document.querySelector('#titSugerencia').remove();
    };



    document.querySelectorAll('.hidden').forEach((item) => {
      item.removeAttribute('class');
    });


    for (var i = 0; i < tomas; i++) {

      let workingDate = new Date(`January 2, 1989 ${selectedTime}:00`);
      workingDate.setHours(workingDate.getHours() + (i * selection));
      let workingTime = extractTime(workingDate);
      let especial = ["3", "4"].includes(selection) && workingTime[0] < 6;
      let espClass = especial ? "especial" : "";
      let espNote = especial ? "<span> (Opcional)</span>" : "";
      let am = workingTime[0] <= 12;
      let horaFinal = t24 ? workingTime[0] : am ? workingTime[0] : workingTime[0] - 12;
      let minutosFinal = workingTime[1];
      let ampm = t24 ? "" : am ? "<span>a.m.</span>" : "<span>p.m.</span>";

      list.insertAdjacentHTML('beforeend', `<li class="${espClass}">
      <span>${horaFinal}</span><span>:</span><span>${minutosFinal}</span>
    ${ampm+espNote}
      </li>`);
    };

    let sug6;
    let sug8;

    if (t24) {
      sug6 = `<li><span>6:00</span></li>
<li><span>12:00</span></li>
<li><span>18:00</span></li>
<li><span>24:00</span></li>`;

      sug8 = `<li><span>7:00</span></li>
<li><span>15:00</span></li>
<li><span>23:00</span></li>
<hr/>
<li><span>8:00</span></li>
<li><span>16:00</span></li>
<li><span>24:00</span></li>`;
    } else {
      sug6 = `<li><span>6</span><span>:</span><span>00</span><span>a.m.</span></li>
<li><span>12</span><span>:</span><span>00</span><span>p.m.</span></li>
<li><span>6</span><span>:</span><span>00</span><span>p.m.</span></li>
<li><span>12</span><span>:</span><span>00</span><span>a.m.</span></li>`;
      sug8 = `<li><span>7</span><span>:</span><span>00</span><span>a.m.</span></li>
<li><span>3</span><span>:</span><span>00</span><span>p.m.</span></li>
<li><span>11</span><span>:</span><span>00</span><span>p.m.</span></li>
<hr/>
<li><span>8</span><span>:</span><span>00</span><span>a.m.</span></li>
<li><span>4</span><span>:</span><span>00</span><span>p.m.</span></li>
<li><span>12</span><span>:</span><span>00</span><span>p.m.</span></li>`;
    }
    if (["6", "8"].includes(selection)) {
      document.querySelector('ul#sugerencia').insertAdjacentHTML('beforebegin', '<h3 id="titSugerencia">También se sugieren estos horarios:</h3>');
    }
    let sugerencia = selection == "6" ? sug6 : selection == "8" ? sug8 : "";
    document.querySelector('ul#sugerencia').insertAdjacentHTML('beforeend', sugerencia);

  };
};



inputs.forEach((input) => {
  input.addEventListener('change', hoursHandler)
});

              
            
!
999px

Console