Search code examples
javascriptnode.jsdateherokunan

`date.getHours` returns NaN inside a Node.js request


I am using Node.js and Heroku for my server-side and my request responses are based on the given time of the day. So I am trying to get the hours and minutes every time the user makes a request as the following:

app.post('/heroku-url', (request, response, next)=>{
  const estTime = new Date().toLocaleString('en-US', {hour12:false, timeZone:"America/New_York"});
  const day = new Date(estTime);
  
  console.log("My Hours is "+ day.getHours())
})

However, this logs NaN in the console. If I move it outside of the request, it returns the hour from which the dynos got restarted which is not what I want. Any help with this issue?


Solution

  • This is all you need, the estTime is completely useless and makes the Date object invalid.

    app.post('/heroku-url', (request, response, next)=>{
      const day = new Date();
      
      console.log("My Hours is "+ day.getHours())
    })