Help with getting data "from" date "to" date Apis and Microservices Projects - Exercise Tracker

Part of the project is : “5. I can retrieve part of the log of any user by also passing along optional parameters of from & to or limit. (Date format yyyy-mm-dd, limit = int)”

In my database I have a user with no exercises on 2019-07-01, but there are exercises on 2019-07-02.

This link https://swamp-liquid.glitch.me/api/exercise/log?userId=nI5SyLWip&from=2019-07-01&to=2019-07-02 returns:" {“username”:“John Doe”,“Exercise Count”:0,“Exercise Log”:}"

Because there are exercises on 2019-07-02, what I expected it to return is:
“{“username”:“John Doe”,“Exercise Count”:3,“Exercise Log”:[{“date”:“2019-07-02T21:18:48.946Z”,“description”:“pull ups”,“duration”:“90”},{“date”:“2019-07-02T21:22:30.395Z”,“description”:“push ups”,“duration”:“90”},{“date”:“2019-07-02T22:19:37.790Z”,“description”:“push ups”,“duration”:“50”}]}”

What I have to do to get the expected return is make the “to” one day past what I want to return like this https://swamp-liquid.glitch.me/api/exercise/log?userId=nI5SyLWip&from=2019-07-01&to=2019-07-03.

Why are the exercises on the “to” date not returned with my code? Full code here.

//get list of user requested exercises here
app.get("/api/exercise/log", function (req, res) {
    var userId = req.query.userId;
    var from = req.query.from ? new Date(req.query.from) : new Date("1970-01-01");
    var to = req.query.to ? new Date(req.query.to) : new Date();

    User.findById(userId, function (err, doc) {
      if (!doc) {
        res.send({ "error": "userId not found" });
      } else {
        var exerciseLog = doc.exercises.sort((a, b) => a.date.getTime() - b.date.getTime())
          .filter(x => x.date >= from && x.date <= to);
        var limit = !isNaN(req.query.limit) ? req.query.limit : exerciseLog.length;
        exerciseLog = exerciseLog.slice(0, limit);
        res.send({ "username": doc.username, "Exercise Count": exerciseLog.length, "Exercise Log": exerciseLog });
      }
    });
  });

This comes up on Stack Overflow fairly regularly (if indeed your problem is what I think it is), and the post that starts with this

There are several crazy things that happen with a JS DATE object that convert strings, for example consider the following date you provided

Note: The following examples may or may not be ONE DAY OFF depending on YOUR timezone and current time.

may help. The link should take you to the page with the relevant post at the bottom (currently 192 upvotes)

@willjw3 thanks for the response. I had looked at stackoverflow and then looked at your link also. There are functions to manipulate the date and ways to correct for the time zone. But in other examples of this project I looked at no one had to manipulate the date to return the expected result.

For example someone else’s project I tested (created a user and put exercises on 2019-07-02) their code works with out special manipulation: https://javineya-fcc-exercise-tracker.glitch.me/api/exercise/log?userId=5d1d575f4c7c58007a67e545&from=2019-07-01&to=2019-07-02. This returns “{“user”:“John Doe”,“total”:2,“exercise”:[{“description”:“pull ups”,“duration”:20,“date”:“2019-07-02”},{“description”:“pull ups”,“duration”:20,“date”:“2019-07-02”}]}”

Their code is:

app.get( "/api/exercise/log", ( req, res, next ) => {
  // store all necessary variables
  let userId = req.query.userId;
  let queries = {
    from: req.query.from,
    to: req.query.to,
    limit: req.query.limit
  };
  
  // find the correct user by ID
  dbUser.findById( userId, ( error, user ) => {
    if ( !user ) { 
      res.send({ "Error": "User not found." })
      
    } else { 
      let results = user.exercise;
      
      if ( queries.from && queries.to ) {
        results = results.filter(( exercise ) => ( 
          exercise.date >= queries.from && exercise.date <= queries.to 
        ));
        
      } else if ( queries.from ) {
        results = results.filter(( exercise ) => ( 
          exercise.date >= queries.from 
          
        ))
      }
      
      if ( results.length > queries.limit ) {
        results = results.slice( 0, queries.limit );
      }
      
      res.send({
        user: user.username,
        total: results.length,
        exercise: results
      });
    }
  });
});

There full code is here.
They didn’t do anything to manipulate the date, I’m not seeing the way in which their code differs from mine that makes there’s work correct and mine does not.

I figured out the problem. It was with the time part of the date. In search to 2019-07-4
this is excluded:
2019-07-04T21:59:56.767+00:00
and this is included:
2019-07-04T00:00:00.000+00:00

So, since time is not important in this case I just had to use .setHours(0, 0, 0, 0) to make the time zeroed out.