Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Trying to pass an object as a parameter is breaking the variable. Should I be refactoring this?

Is it possible to pass a date object as a parameter? I was attempting to set up another set interval to refresh the greeting for automatic updates when I noticed I was using a new Date() a lot. I tried to refactor the code a bit but ended up breaking things. My new code does not recognize the 'today' variable even for the first selector where it was working prior.

//Variables
const today = new Date();
const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

//Selectors
document.getElementById("day").innerHTML = weekday[today.getDay()];
document.getElementById("date").innerHTML = today.toLocaleDateString();

//Functions

//Greeting based on local time
function timeOfDay(today) {
  document.getElementById("greeting").innerHTML =
    today.getHours() < 12
      ? "Good Morning"
      : today.getHours() >= 18
      ? "Good Evening"
      : "Good Afternoon";
}
  setInterval(timeOfDay, 1000 * 60 * 60);

//Get and display local time
function getTime(today) {
  const currentTime = today.toLocaleTimeString();
  document.getElementById("time").innerHTML = currentTime;
}
setInterval(getTime, 1);

The code below was working fine I was just trying to see if there was another way to do it without constantly creating a new date object.

//Greeting based on local time
document.getElementById("greeting").innerHTML =
  new Date().getHours() < 12
    ? "Good Morning"
    : new Date().getHours() >= 18
    ? "Good Evening"
    : "Good Afternoon";

//Get and display local time
const today = new Date();
const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
document.getElementById("day").innerHTML = weekday[today.getDay()];
document.getElementById("date").innerHTML = today.toLocaleDateString();
function getTime() {
  const time = new Date();
  const currentTime = time.toLocaleTimeString();
  document.getElementById("time").innerHTML = currentTime;
}
setInterval(getTime, 1);

Any help or advice is appreciated.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The issue here is that when you create a new Date object, its value is the time it was created. It doesn't change after that.

Thankfully there is an easy solution.

You don't need to create a new Date object everytime, just use the object itself as a function.

var now = new Date();
setInterval(() => {
  console.log(`new Date(): ${now}`);
  console.log(`Date(): ${Date()}`);
  console.log("");
}, 1000)

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda