// running tests Your project can handle dates that can be successfully parsed by
new Date(date_string)
// tests completed
After testing result does not feel different but the problem does not change.
let responseObject = {}
app.get('/api/timestamp/:date_string', (request, response) => {
let date_string = request.params.date_string
if (date_string.includes('-')) {
//if it is date string create time stamp
responseObject['unix'] = new Date(date_string).getTime()
responseObject['utc'] = new Date(date_string).toUTCString()
}
else {
date_string = date_string / 1000
date_string = date_string * 1000
responseObject['unix'] =date_string
responseObject['utc'] = new Date(date_string).toUTCString()
}
if (!responseObject['unix'] || !responseObject['utc']) {
return response.json({ error: "Invalid Date" })
}
return response.json(responseObject)
})
app.get('/api/timestamp', (request, response) => {
responseObject['unix'] = new Date().getTime()
responseObject['utc'] = new Date().toUTCString()
return response.json(responseObject)
})
I’d like to help but your question is not clear.
- Can you link to the question?
- Can you try to explain what you expect and what you actually get?
I can not pass the request of: running tests Your project can handle dates that can be successfully parsed by
new Date(date_string)
all other requirement have been meet but I can not understand why it does not pass the test.
the only way i managed to pass the code for the given require meant ( running tests Your project can handle dates that can be successfully parsed by
new Date(date_string) ) was when I did not follow this format
date_string = date_string / 1000
date_string = date_string * 1000
responseObject['unix'] =date_string
responseObject['utc'] = new Date(date_string).toUTCString()
I did this
responseObject['unix'] =new Date(date_string).getTime()
which can not pass the error story.
If you can post a link to the project on repl.it or somewhere similar, you may get more help debugging the problem. Even though these snippets of code look like they may work or are close to working, it is much harder to solve these kinds of problems without all of the code and a live project to test.
Good luck.
https://repl.it/@NiloyKhastagir/boilerplate-project-timestamp
In your /api/timestamp/:date_string route, you should add a
console.log(request.params.date_string);
at the very beginning of your route, and then everywhere you change your response object, you should add
console.log(responseObject);
That way, you can monitor the input and the output in the console as you run the fCC tests against your API. If you do that, you will see that the tests are requesting
2016-12-25
1451001600000
05 October 2011
this-is-not-a-date
and I believe you are not handling the third entry correctly.
Good luck.
Okay I will try again and thank you for the advice.
I may ask other question again.
Hi Guys,
After some confusion with this project, I finally worked out the problem with passing the 1451001600000 test – node does not successfully parse strings the same way as numbers using the Date constructor… Also, one issue that isn’t directly related here that is if you need to parse dates on the client side, browsers implementations are very different so it is recommended to use a library such as moment.js, date-fns, dayjs etc.
If the clue of strings vs number wasn’t enough you can see my solution here: https://repl.it/@svargasdev/boilerplate-project-timestamp#server.js
Good luck!