Basic Node & Express 8/12 - Locale Now Date Problems

Tell us what’s happening:
I’m passing only the first of the two tests in this challenge.
The test I’m failing:

The /now endpoint should return a time that is +/- 20 secs from now

I thought it was because of the timezone, so I added the spanish locale since I’m coding from Spain, but still haven’t passed the test.

My computer hour and the one in the “/now” endpoint are showing equal, though I cannot see the seconds since I’m on coding on a mac.

Your code so far

app.get("/now", function ( req, res, next){
  
  let date = new Date().toLocaleString("es-ES", {timeZone: "Europe/Madrid"});
  
  req.time = new Date(date).toString();
  next();
  
}, function (req, res) {
  
  res.json({ time: req.time });
  
})

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36.

Challenge: Basic Node and Express - Chain Middleware to Create a Time Server

Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/chain-middleware-to-create-a-time-server

Have you tried by not passing a parameter in a Date Object.
just set req.time = new Date().toString();
Hope that help

Hi @Abbas-000 thanks for your quick response,

Yes that is the first thing I tried, and it doesn’t pass the test.

Since we are 1 hour ahead of Greenwich Time in Spain I’m thinking that the problem is related to the fact that the computer’s time and the new Date() object on Glitch, do not match…

Now I’m trying to use moment and moment-timezone npm packages like this:


app.get("/now", function ( req, res, next){
  
  let mDate = moment(new Date()).tz("Europe/Madrid").format("hh:mm:ss");
  
  req.time = mDate;
  next();
  
}, function (req, res) {
  
  res.json({ time: req.time });
  
})

It’s showing nicely on the app’s endpoint, but it still isn’t passing the second test…

PS: I’ve also tried other USA timezones like “America/New_York” or “America/Los_Angeles”, just in case… same result…

Can you send me the link to your project?
And if you your using windows 10 see in the settings > time & language and see if the time is set automatically and below in the dropdown select the region you are in.

Hi I just passed it, but with a little “hack”.

Had to change the timezone of my computer to greenwich time, then use the default date object converted to string new Date().toString()

then everything went fine.

I found out that somehow the delay in time was of a minute more or less (when I was using my local time), I’m guessing maybe because some delay in the data getting to my computer from the servers of Glitch?

Though this delay also disappeared using greenwich time.

Just in case, I leave the link to my project here:

PS: I’m using mac OS with automatic time detection, but had to manually set the time-zone to London time.

I am glad to that you passed the challenge :smiley:

Yes, thanks for being there to help :slightly_smiling_face::+1: