Timestamp Microservices - dont understand "Your project can handle dates.." test

Hej,

I dont understand the “Your project can handle dates that can be successfully parsed by `new Date(date_string)”-test in the API timestamp project. I pass every other test except this one and it feels very counter intuitive.

My Code:

const express = require('express');
var dayjs = require('dayjs')
//import dayjs from 'dayjs' // ES 2015
dayjs().format()
const app = express();
// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC 
const cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 }));  // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(req, res) {
  res.sendFile(__dirname + '/views/index.html');
});

app.get("/api/", (req, res) => {
    res.json({ "unix": new Date().getTime(), "utc": new Date().getTime() })
})

app.get("/api/:date", (req, res) => {
  const rawDate = req.params.date;
  console.log(rawDate, new Date(rawDate))
  if (dayjs(rawDate).isValid() && new Date(rawDate)) {
    //create regexp-obj matching yyyy-mm-dd with date restrictions
    const regexp_ymd = /^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/;
    if (regexp_ymd.test(rawDate)) {
      const year = rawDate.slice(0, 4);
      const month = rawDate.slice(5, 7);
      const day = rawDate.slice(8);
      const unix = Date.parse(rawDate);
      const utc = new Date(Date.UTC(year, month - 1, day));
      res.json({ unix: unix, utc: utc.toUTCString() });
    } else if (Number(rawDate) >= 0) {
      const utcMiliseconds = Number(rawDate / 1000);
      const d = new Date(0); // declare date d at start of epoch time (1970)
      d.setUTCSeconds(utcMiliseconds);
      res.json({ unix: Number(rawDate), utc: d.toUTCString() });
    }
    else {
      res.end();
    }
  } else {
    res.json({ error: "Invalid Date" })
  }

});


// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

It means that you should let new Date() parse all the dates and that you don’t need any external libraries like dayjs or moment. In particular it means you should be able to handle 05 October 2011 (usually this one is the problem and your regular expression doesn’t match it). You should see it in the console where you log rawDate; just log the route responses to see what is being returned and you should be able to fix it.

1 Like

Também estou tendo o mesmo problema… esse código não passa . Tenta mudar o usuário e senha do replic e tenta novamente…

Thanks!
What do you mean with logging the route responses?
Do you mean the response-object?

The first thing to do to debug any express route problems is to log all the route inputs and all the responses. That means at the start of every route you may have a problem with you need something like

console.log(`req.body:  ${JSON.stringify(req.body)}`);
console.log(`req.params:  ${JSON.stringify(req.params)}`);
console.log(`req.query:  ${JSON.stringify(req.query)}`);

and before every response you need something like

console.log('whatever your response is, usually some JSON object for these projects');

You should also return responses in general (return res.json({...});) so that you don’t inadvertently send multiple responses or reach the end of a route without returning anything. If you log the input and output, you can compare the output to the specifications and usually get some idea as to why your output from the given input doesn’t match what the tests expect. Finally, this should be a starting point since you will often need to log more information in the route to find the problem.