Basic Node and Express - Chain Middleware to Create a Time Server(2)

Basic Node and Express - Chain Middleware to Create a Time Server(2)

I’m getting:
the returned time is not between ± 20 secs from now;
I noticed that the new Date() returns GMT -0, which is not my browser time and for that reason the test might fail,
I read how to change it and managed to sync it as you can see below but it still won’t work.
Please advise…

req.time =  new Date(new Date().toString()+"-0300").toGMTString();
        function(req, res) {
  res.send({time: req.time});

new Date() Object automatically grabs current time based on your timezone. Therefore there is no need to add any string numbers for it. Go head and simply create a new Date. Then strigify it and save it in req.time.

req.time = new Date().toString();

it didn’t work at the first place when i used just “new Date()” , that’s the reason i started to complex everything

Any update on this? I’m having the same issue.

It’s a timezone issue in your PC, for me, I just set the timezone and time to automatic and that solve the issue.

My code was:

app.get('/now',(req,res,next)=> {
    req.time = new Date().toString();
  }, (req,res)=> {

Hey guys, 3 hours to solve this xD

The problem is not timezone, the problem is the local system clock and the server clock is not syncronized. My machine was about 40 seconds delayed in time, and is not possible to change the seconds in system clock, i tried to set an automatic clock from internet and other aproaches but nothing was working, so i did that:

const second = 1000
function timeMiddleware (req, res, next){
  let timeshift = 20 * second
  let now = new Date()
  req.time = new Date(now.getTime() + timeshift)
function timeHandler (req, res){
  res.json({time: req.time})
app.get("/now", timeMiddleware, timeHandler) //time: "Wed Oct 09 2019 20:50:58 GMT+0000 (Coordinated Universal Time)"

And works