Node/Express Timestamp : Using req.query to grab date and turn it into UNIX/UTC timestamp

I’m working on the Timestamp API project and I’m stuck.

Here is my app.get function:

app.get("/api/timestamp/:date_string?", function (req, res, date) {
  // If query params are empty use the current date
  if(Object.keys(req.query).length === 0) {
    date =;
    console.log(" outputs : " + date);
    var dateUTC = new Date().toUTCString();
      unix: date,
      utc: dateUTC
  // If query params are provided use them
  else {
      // How do I take req.query here and turn them into UNIX/UTC timestamps?
      unix: req.query,  
      utc: req.query

In the else part of the if/else statement I am grabbing 2015-12-25 from the URL via req.query. After I grab it how can I turn it into UNIX/UTC timestamps inside my res.json function?

Something like new Date(req.query)? I’ve tried that and other variations of date functions and can’t seem to figure it out.

Here is what my current code outputs:

You can write new Date("2011-01-02").getTime() / 1000

I see you that your callback function (req, res, date) {... for app.get function has date as a parameter and then you are reassigning it right away anyways.

Then you are using both route parameters and query parameters to try and get the date string. Your URL is just getting it from the query.

If you want to get the data through route parameters:

app.get("/api/timestamp/:date_string", function (req, res) {

And the url would simply be https://[hostname]/api/timestamp/2015-12-25

To get the date from a query:

app.get("/api/timestamp", function (req, res) {

URL https://[hostname]/api/timestamp?date=2015-12-25

Or I guess not using a name for the value puts it directly on the query obj.


You can write new Date("2011-01-02").getTime() / 1000

The problem with doing it like that is I can’t hardcode the date. I need to create a new date object based on the value of req.params.date_string. How do I do that? It won’t let me pass it like this:

      unix: new Date(req.params.date_string)

Also, if I ditch using req.query this effects the boolean logic I am trying to use in the if statement. I would have to change this:

if(Object.keys(req.query).length === 0)

How would I do that if I went with the req.params approach?

Do what’s working for you I was just pointing out what I noticed.

Are you certain that the variable you are passing to new Date is what you think it is?

const mydate = '2015-02-12';
const date = new Date(mydate);

If this is what you are trying to do you can see that this works.

Have you looked at the hint if that challenge?

You will need to create the ‘/api/timestamp/’ endpoint separately from the endpoint that reads the date to be parsed from the URL. You won’t need a conditional to deal with this endpoint.

typeof req.params.date_string is string. But one test case uses unix timestamp. Think about how to check it.

Read it and find out what to put in new Date().

Hope it helps!