# Issue in APIs and Microservices

**URL:** <https://forum.freecodecamp.org/t/issue-in-apis-and-microservices/368330>\
**Category:** General\
**Created:** [April 5, 2020, 4:52am UTC](https://forum.freecodecamp.org/t/issue-in-apis-and-microservices/368330 "2020-04-05T04:52:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![githubashutoshsoni](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/githubashutoshsoni/32/140111_2.png) [@githubashutoshsoni](https://forum.freecodecamp.org/u/githubashutoshsoni)\
**Post date:** [April 5, 2020, 4:52am UTC](https://forum.freecodecamp.org/t/issue-in-apis-and-microservices/368330/1 "2020-04-05T04:52:20Z")

</div>

So, I’m doing the microservice project where I have to get unix date timestamp . No test cases are being passed…

```auto
app.get("/api/timestamp/:date_string", function(req, res) {
  // {"unix":1450137600000,"utc":"Tue, 15 Dec 2015 00:00:00 GMT"}
  var date = req.query.date_string;
  console.log(date);
  const epoch = parseInt(date, 10);
  console.log("Hello");
  if (isNaN(epoch)) {
    console.log("isNan");
    var dateObject = new Date(date);

    if (dateObject.toString() === "Invalid Date") {
      res.json({ error: "Invalid Date" });
    } else {
      var returnObject = {
        unix: dateObject.valueOf(),
        utc: dateObject.toUTCString()
      };
      res.json(returnObject);
    }
  } else {
// if the returned is a number then convert into epoch and show the result..
    console.log("is else");
    var returnObject = { unix: epoch, utc: new Date(epoch).toUTCString() };
    res.json(returnObject);
  }
});

```

when I try to enter some query parameter in the URL like date\_string=1450137600000  
the unix timestamp changes to 1586061823000.  
and that’s the reason why I think it is happening. How should I resolve this.  
Thanks

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [April 5, 2020, 5:01am UTC](https://forum.freecodecamp.org/t/issue-in-apis-and-microservices/368330/2 "2020-04-05T05:01:22Z")

</div>

1450137600000 would be the 13th of January 47923 anyway, so give it something sensible (your number is at least 1000 times bigger than it should be) and it will work.

---

<div class="post-metadata">

**Author:** ![githubashutoshsoni](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/githubashutoshsoni/32/140111_2.png) [@githubashutoshsoni](https://forum.freecodecamp.org/u/githubashutoshsoni)\
**Post date:** [April 5, 2020, 8:12am UTC](https://forum.freecodecamp.org/t/issue-in-apis-and-microservices/368330/3 "2020-04-05T08:12:43Z")

</div>

So, I divide the number that I convert to int by 1000 and it should fix? I tried that but still no test cases are passed.

---

<div class="post-metadata">

**Author:** ![githubashutoshsoni](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/githubashutoshsoni/32/140111_2.png) [@githubashutoshsoni](https://forum.freecodecamp.org/u/githubashutoshsoni)\
**Post date:** [April 5, 2020, 8:17am UTC](https://forum.freecodecamp.org/t/issue-in-apis-and-microservices/368330/4 "2020-04-05T08:17:16Z")

</div>

BTW i looked into the solution as well… and I modified my code it as the following

```auto
app.get("/api/timestamp/:date_string", function(req, res) {
  
  var dateString= req.query.date_string;
  
  if (/\d{5,}/.test(dateString)) {
  var dateInt = parseInt(dateString);
    //Date regards numbers as unix timestamps, strings are processed differently
    res.json({ unix: dateString, utc: new Date(dateInt/1000).toUTCString() });
  }
  
  var dateObject= new Date(dateString);
  if(dateObject.toString()==="Invalid date"){ 
    res.json({error: "Invalid date"});
  }
  else{
    res.json({unix:dateObject.valueOf,utc:dateObject.toUTCString()})
  }
});

```

still no test cases are being passed… can you please tell me what am i doing wrong thanks.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:00pm UTC](https://forum.freecodecamp.org/t/issue-in-apis-and-microservices/368330/5 "2021-06-01T19:00:13Z")

</div>

Closed on June 1, 2021, 7:00pm UTC


