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.

1 Like

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}

Howdy, @adam-weiler . You’ve probably guessed by now, but for anyone else coming across this…

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

Values from URLs are strings.

I worked the problem much like you did, since the Glitch console didn’t print anything, I just tested stuff in the Chrome dev tools console. This is what I tested:

var x = '12-01-2015';
Number(x) // NaN
isNaN(Number(x)) // true
x = '1450137600';
Number(x) // 1450137600
isNaN(Number(x)) // false
1 Like

Thanx! Yes I figured it out and was able to finish this project about 3 or 4 weeks ago. It makes sense, since URLs are strings and even if they contain numbers they’d still be “string” characters.

The dev tools console is a good idea too. I tend to console.log a lot just to tell what’s going on!


If you like console.log(...), just wait – there’s more! In addition, you can send error messages with console.error(...), possible problems with console.warn(...), generic info with console.info(...)… Yup, console are your FRIEND!! I mean, you can do all those with console.log, but you get pretty colors with warn and error, so. :wink: