"handle a valid unix date, and return the correct unix timestamp"

Tell us what’s happening:
My question is in regards to " Timestamp Micro service". All the test are coming back positive except this one:

“handle a valid UNIX date, and return the correct UNIX timestamp”

My code seems to handle date format such as 2020-08-19 but seems to have problems with the UNIX equivalent “1597708800”.

How do I recognize this format?

Your code so far

app.get("/api/timestamp/:date_string?", (req,res)=>{
  var timeParam = req.params.date_string;
  // console.log(timeParam.toString());
  
  if (timeParam) {
    console.log("First");

    var date = new Date(timeParam);
    var testParam = date.toString();
    console.log(testParam);
    // Not empty but invalid date 
    if(testParam == "Invalid Date"){
      console.log("Invalid date test");
      return res.json({error : "Invalid Date" });
    }

    //Not empty and valid date
    console.log("Valid Date Test");
    
    var currentTime = date.getTime();
    var currentUTC = date.toUTCString();
    return res.json({unix : currentTime, utc : currentUTC });
  }
  
    //empty string scenario
    console.log("Empty string test");
    var date = new Date();
    var currentTime = date.getTime();
    var currentUTC = date.toUTCString();
    res.json({unix : currentTime, utc : currentUTC });
});

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.

Challenge: Micro Service Time Stamp

Link to the challenge:

I think your only problem is that the epoch time is a string. You can create a Date object with a timestamp, but it must be a number.

screenshot of console showing the results of an integer argument and a string argument

@ArielLeslie

Thanks for you help. I ended up doing this and it “passes” the tests.

It’s not pretty and it definitely needs to be refactored. :sweat_smile:

app.get("/api/timestamp/:date_string?", (req,res)=>{
var timeParam = req.params.date_string;

//Testing if NOT EMPTY
if (timeParam) {

var date = new Date(timeParam);
var testParam = date.toString();
console.log(testParam);

//If Invalid date. It could mean it's in milliseconds.
if(testParam == "Invalid Date"){

  // Needs to test if it has valid millisecond time stamp 

  
  let reg = /^\d+$/;
  let numbersOnlytest = reg.test(timeParam);

  
  if(numbersOnlytest) {
    
      var parseIntDate = parseInt(timeParam);
      let tbd = new Date(parseIntDate).toUTCString() 
   
      return res.json({unix : parseIntDate, utc : tbd });
    }
  return res.json({error : "Invalid Date" });
}



// NOT Invalid Date
var currentTime = date.getTime();
var currentUTC = date.toUTCString();
return res.json({unix : currentTime, utc : currentUTC });

}

//empty string scenario WORKS
console.log("Empty string test. Geat success.");
var date = new Date();
var currentTime = date.getTime();
var currentUTC = date.toUTCString();
res.json({unix : currentTime, utc : currentUTC });

});

In your opinion when is the best time to refactor code after a challenge? I don’t want to look at my peers answers on this challenge until I refactor at least once and secondly ask for feed back.

Instead of trying to create the date twice and creating two different returns, I would check the timeParam for formatting before creating a Date object. Then you don’t need different logic for the timestamp vs string.