Tell us what’s happening:
I looked at the tests on GitHub, and apparently it checks for equality between data returned from my endpoint and Date.now() with a range of +/- 20 seconds. Now, when I manually test, my code doesn’t return a time & date close to the range in the test.
What can I do about this?
Thanks for your time.
Your code so far
app.get("/api", (req, res) => {
const now = new Date();
res.json({ unix: now.getTime(), utc: now.toUTCString() });
});
app.get("/api/:date", (req, res) => {
let inputDate;
if (!/\D/.test(req.params.date)) {
inputDate = new Date(Number(req.params.date));
} else {
inputDate = new Date(req.params.date);
}
if (inputDate.toString() === “Invalid Date”) {
return res.json({ error: “Invalid Date” });
}
const unix = inputDate.getTime();
const utc = inputDate.toUTCString();
res.json({ unix, utc });
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
If you can post a link to your project on repl.it (or similar) so we can see and run the code, you may get more help.
In general, this project can encounter timezone offset problems so if your dates are consistently off by your offset (mine is 6 hours in US central time for instance) that could be your problem.
Even better, console.log() your inputs to the second route (req.params.date) and outputs to both routes (log your JSON everywhere you return JSON) to see what is going into and coming out of the routes while you run the fCC tests against your project. Usually that is the best clue as to the problem.
now, i think you’re right. my problem is a timezone problem. when testing this:
app.get("/api", (req, res) => {
const now = new Date();
res.json({ unix: now.getTime(), utc: now.toUTCString() });
});
…the json response always return the time -2 hours from my actual time.
then, i tried this:
app.get("/api", (req, res) => {
const now = new Date(Date.now() + ( /* a calculation to get my actual time */ 7200000 - 150000));
res.json({ unix: now.valueOf(), utc: now.toUTCString() });
});
…and it does return the correct time but when tested in fcc, it still doesn’t work!
what can i do about this?
Your first block (const now = new Date();) is correct. I cloned your project and ran it from my end and passed all the tests. This is likely a timezone issue and it’s probably your computer that is not configured correctly because the glitch and fCC servers are running in UTC. So, check your computer and make sure that your timezone settings are correct, from the hardware clock, to the system clock, to the displayed time. On a unix system, it’s usually best if the hardware and system clocks are in UTC and your OS/user programs display the appropriate offset time for your timezone. Since your code is returning 2 hours off, look for one of the three clocks that is two hours off.