Timestamp Microservice - Api project

Timestamp Microservice - Api project


I’m working on the Api project for Timestamp Microservice. Here’s my code sofar:

Right now these 3 features are working:
Input: (empty) -> Output: {"unix":"2018-12-08(current time)","utc":"Sat, 08 Dec 2018 (current time)"}

Input: 1450137600 -> Output: {"unix":1450137600,"utc":"1970-01-17T18:48:57.600Z"}

Input: 1450137600abcdefg -> Output: {"unix":null,"utc":"Invalid Date"}

My questions is this:
-Is the Api supposed to accept 2015-12-25 as a valid input?

Project description: https://learn.freecodecamp.org/apis-and-microservices/apis-and-microservices-projects/timestamp-microservice/


So, according to the page itself,

  1. A date string is valid if can be successfully parsed by new Date(date_string) .
    Note that the unix timestamp needs to be an integer (not a string) specifying milliseconds .
    In our test we will use date strings compliant with ISO-8601 (e.g. "2016-11-20" ) because this will ensure an UTC timestamp.

So does new Date("2015-12-15") parse correctly?

According to their sample URLs, that is something your API handler should accept, yes.


Okay, right. I think I was a bit confused by the instructions.

1: When you pull values from the URL, are they strings or numbers?

2: The console.log doesn’t seem to work inside the get function. So I’ve been testing my code outside the get function:

var case1 = "2015-12-25";
var case2 = 1450137600;
var case3 = "1450137600abcdefg";

var output1 = new Date(case1); //Convert to a date.
var output2 = new Date(case2);
var output3 = new Date(case3);

var reput1 = output1.getTime(); //Get time from date.
var reput2 = output2.getTime();
var reput3 = output3.getTime();

Which produces:

{"unix":1451001600000, "utc":"Fri Dec 25 2015 00:00:00 GMT+0000 (Coordinated Universal Time)"}
{"unix":1450137600, "utc":"Sat Jan 17 1970 18:48:57 GMT+0000 (Coordinated Universal Time)"}
{"unix":NaN, "utc":"Invalid Date"}
{"unix":reput, "utc":output}

But when I try to use that code inside the get function, I’m getting:


It’s working for case1, but not case2 or case3.

Originally I had something like this. It worked on case2 and case3 (with an else statement), but not for case1:

var output = Number(req.params.input); //Convert input to a number.
var reput = new Date(output); //Create a new date.
reput.toUTCString(); //Convert to UTCString.
{"unix":reput, "utc":output}