Tell us what’s happening:
I got all the tests to work except for the ‘error’ one.
This is my thoughts:
- get input of user
- define valid date string (new Date(<user’s input>))
- if date string is valid and is 1479663089000, return correct json
- if date string is valid and is “2016-11-20”, return correct json
- if date string is invalid, return error in json
Am I wrong in my logic?
Your code so far
// no parameters
app.get(‘/api/timestamp/’, (req, res) => {
res.json({
‘unix’: new Date().getTime(),
‘utc’: new Date().toUTCString()
});
})// with parameters
app.get(‘/api/timestamp/:date_string?’, (req, res) => {const {date_string} = req.params; // user’s input
// make valid date
let date = new Date(date_string);
// console.log(date: ${date}, date_string: ${date_string}
)// if date is valid
// user input is 1479663089000
if (typeof(parseInt(date_string === ‘Number’)) && date.toString().length <= 16) {
res.json({
unix: new Date(parseInt(date_string)).getTime(),
utc: new Date(parseInt(date_string)).toUTCString()
})
}// user input is 2016-11-20
if (date) {
res.json({
unix: new Date(date).getTime(),
utc: new Date(date).toUTCString()
})
}// if date is not valid
if (!date) {
res.json({
“error” : “Invalid Date”
})
}
})
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36
.
Challenge: Timestamp Microservice
Link to the challenge: