Back End Development and APIs Projects - Timestamp Microservice

ive formatted the whole json object but i cant seem to pass these 3 tests:

    • A request to /api/:date? with a valid date should return a JSON object with a unix key that is a Unix timestamp of the input date in milliseconds (as type Number)
  1. Failed:A request to /api/:date? with a valid date should return a JSON object with a utc key that is a string of the input date in the format: Thu, 01 Jan 1970 00:00:00 GMT

*3.Failed:A request to /api/1451001600000 should return { unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }

the api works well. this is the link to the project.
https://boilerplate-project-timestamp-4.ahmusa118.repl.co

this is the code

app.get("/api/:date?", function (req, res) {
  const dateObject = new Date(req.params.date * 1000)
  const unmod=new Date(req.params.date/1)
 const weekday= dateObject.toLocaleString("en-US", {weekday: "short"}) // Monday
const month=dateObject.toLocaleString("en-US", {month: "short"}) // December
const day=dateObject.toLocaleString("en-US", {day: "numeric"}) // 9
const year=dateObject.toLocaleString("en-US", {year: "numeric"}) // 2019
const hour=dateObject.toLocaleString("en-UK", {hour: "numeric"}) // 10 AM
const minute=dateObject.toLocaleString("en-US", {minute: "numeric"}) // 30
const second=dateObject.toLocaleString("en-US", {second: "numeric"}) // 15
const tz=dateObject.toLocaleString("en-US", {timeZoneName: "short"})
  let filtered=hour.replace(/\D+/,"")
  function convert(num){
    if(num.length<2){
      return "0"+num
    }return num
  }const x=convert(day)
 const h=convert(filtered)
const m=convert(minute)
const s=convert(second)
  
if(req.params.date==undefined){
 res.json({unix : Date.now(), utc : Date().toString()})
}else if(req.params.date.length==13){
  res.json({unix : parseInt(req.params.date), utc : `${unmod}` })
  
}
else if(unmod=="Invalid Date"){
  res.json({ error : "Invalid Date" })
}
else {res.json({unix : parseInt(req.params.date), utc : `${weekday}, ${x} ${month} ${year} ${h}:${m}:${s} GMT`})}
});

this is the output

{"unix":1451001600,"utc":"Fri, 25 Dec 2015 00:00:00 GMT"}

solution: boilerplate-project-timestamp (4) - Node.js Repl - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - Timestamp Microservice

Link to the challenge:

Look in the browser network tab when you submit. For the 500 if you look at the response or the console you can see the error (check for typos).

This GET /api/05 October 2011, GMT should return a valid date.

okay that worked, the parsing to date string. but im still getting this error.

A request to /api/:date? with a valid date should return a JSON object with a unix key that is a Unix timestamp of the input date in milliseconds (as type Number)

how do i rectify that

Which version are you working on?

This one passes for me

https://boilerplate-project-timestamp-5.ahmusa118.repl.co/

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.