Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
How to fix it with date format?

In this script it tries to do so that when you choose the date and the given format it shows. Of course, something is wrong with validation . If the "Select date format" option is selected, it will show an error. If you choose the other 2 formats, normally you should do

var nazwamiesiecy = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var nazwadni = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

let input = document.querySelector("input");
let guzik = document.querySelector("button");

guzik.addEventListener("click", () => {
  event.preventDefault();

  let wybranaData = new Date(input.value);

  let format = document.querySelectorAll("select").value
  let opcja1 = document.querySelectorAll("option")[0].value
  let opcja2 = document.querySelectorAll("option")[1].value
  let opcja3 = document.querySelectorAll("option")[2].value

  if (!input.value) {
    alert("Enter the date");
    return;
  }

  if (!opcja1) {
    alert("Enter the date format")
    return;
  }

  if (!opcja2) {
    document.getElementById("dataa").innerHTML = wybranaData.getDate() + " " + nazwamiesiecy[wybranaData.getMonth()] + " " + wybranaData.getFullYear().toString() + " roku";
    return;
  }

  if (!opcja3) {
    document.getElementById("dataaa").innerHTML = wybranaData.getDate() + "." + wybranaData.getMonth() + "." + wybranaData.getFullYear().toString();
    return;
  }
})
<form name="">
  <div>
    <label for="txtnum1">Date: </label>
    <input type="date" name="txtnum1" id="txtnum1">
  </div>

  <label for="sztos">Date format: </label>
  <select name="sztos" id="sztos">
    <option value="">Choose a date format</option>
    <option value="op">12/04/2000</option>
    <option value="xd">12 April 2000</option>
  </select>


  <button type="submit">Calculate</button>

  <div id="dataa"></div>
  <div id="dataaa"></div>
</form>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Your opcja tests are wrong,
  • querySelectorAll returns a nodeList Just test the querySelector("select").value
  • your format is with dots, not slashes
  • you are not passing the event in the event listener
  • no need to use toString on numbers when you concatenate

var nazwamiesiecy = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var nazwadni = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

let input = document.querySelector("input");
let guzik = document.querySelector("button");
let dataa = document.getElementById("dataa")
guzik.addEventListener("click", (event) => {
  event.preventDefault();

  if (!input.value) {
    alert("Enter the date");
    return;
  }

  let format = document.querySelector("select").value

  if (!format) {
    alert("Enter the date format")
    return;
  }

  let wybranaData = new Date(input.value);
  
  let dateString = "";
  if (format === "xd") {
    dateString = wybranaData.getDate() + " " + nazwamiesiecy[wybranaData.getMonth()] + " " + wybranaData.getFullYear() + " roku";
  } else if (format === "op") {
    dateString = wybranaData.getDate() + "/" + wybranaData.getMonth() + "/" + wybranaData.getFullYear();
  }
  dataa.innerHTML = dateString;
})
<form name="">
  <div>
    <label for="txtnum1">Date: </label>
    <input type="date" name="txtnum1" id="txtnum1">
  </div>
  <label for="sztos">Date format: </label>
  <select name="sztos" id="sztos">
    <option value="">Choose a date format</option>
    <option value="op">dd/mm/yyyy</option>
    <option value="xd">dd MMMMMM yyyy</option>
  </select>
  <button type="submit">Calculate</button>
  <div id="dataa"></div>
</form>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!