Timestamp Microservice - works with small Unix times, but not large

I’m stumped! My microservice works when I pass it natural dates and it works with smaller Unix stamps, e.g. 222222 works. But 333333 doesn’t work! Somehow, after the higher numbers are getting passed through Date.parse(), they pass the first conditional (isNaN) and I get the object w/ null values.

Any thoughts what could be happening here?

My code:

var express = require("express");

// Build the app
var app = express();

app.use(function (req, res) {
  var param = decodeURIComponent(req.path);
  param = param.slice(1);
  var date = Date.parse(param)/1000;
  var unixInt = parseInt(param, 10);

  var dateObj = new Object();
  
  if(Number.isNaN(date)) {
  dateObj.case = 1;  
  dateObj.unix = null;
  dateObj.natural = null;
  }
  
  else if (isNaN(param) === false) {
    dateObj.case = 2;
    var natDate = new Date (unixInt);
    var natural = natDate.getMonth() + " " + natDate.getDate() + ", " + natDate.getFullYear();
    dateObj.unix = param;
    dateObj.natural = natural;
  }
  
  else {
  dateObj.case = 3;  
  dateObj.unix = date;
  dateObj.natural = param;
  }
   res.send(dateObj);
});

app.listen(8080, function() {
  console.log("runnin");
});

I’m not gonna lie, I deferred to MomentJS and chrono-node modules for this challenge. Date parsing is highly domain specific and I decided mastering it isn’t necessary.

Yeah, MDN basically says not to use Date.parse. I’ll check out MomentJS. Thanks!

1.) MomentJS is awesome.
2.) But I’m STILL having the same problem! Everything is working according to the requirements, EXCEPT unix times in the higher 6 digits (222222 still works, 333333 is still triggering my first condition).

My code:

app.use(function (req, res) {
  var param = decodeURIComponent(req.path).slice(1);
  var dateObj = new Object();
  var unix; 
  var natural; 
  
  if (moment(param).isValid() === false) {
  unix = null;
  natural = null;
  }
  
  else if (isNaN(param) === false) {
  var int = parseInt(param, 10);
  var date = moment.unix(int);
  unix = param;
  natural = date.format("MMMM" ) + " " + date.format("D") + ", " +date.format ("YYYY");
  }
  
  else {
  var date = moment(param);
  unix = date.format("X");
  natural = date.format("MMMM" ) + " " + date.format("D") + ", " +date.format ("YYYY");
  }
   
  dateObj.unix = unix;
  dateObj.natural = natural;
  res.send(dateObj);
});

If you put 3 ` characters above and below your code, it’s easier to read:

function() {
  return true;
}

In addition to moment, I used a library called chrono-node. It is designed to parse many, many different variations of humanized date and time strings. So I can say 3 days ago and it will correctly deduce the date for that.

It also is polite enough to return a falsey value (defined as 0, "", null, and undefined) if it cannot parse a date string. So I found the easier way to tackle this, using these two libraries, was to check to see if I had a unix timestamp. If so, handle that. Remember we’re dealing with a string here, so there is another favorable method for deciding if it reflects a number: Regular expressions.

If not, check to see if chrono can parse the string. If not, we arrive at:

{
  unix: null,
  natural: null
}

Maybe this is useful. I don’t want to give the entire game away.

Also, when using express for this, it may be easier to assign your callback using app.get and specify a placeholder for your path. Checkout the Express docs here:

https://expressjs.com/en/4x/api.html#req

I have no idea what your decodeURIComponent is doing, but it is likely needlessly complicating things.

Let us know how you fare!

1 Like

Thanks for the tips!

I got it working pretty well using regex to test whether or not the parameter is made up of only digits, and then it goes smoothly from there. It doesn’t work with negative unix times yet, so I need to go back and allow for that.

I also need to go back and use app.get instead – thanks for pointing that out. This is the first thing I’ve done with node or express outside of the fcc tutorial challenges, so that’s really helpful.

That’s fantastic! Glad you were successful! Were we supposed to support negative unix times? I may have to go back and add support for that, then…