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

129
Views
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 answers
Answer question

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 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!