Currently, I am in the process of developing an API that allows users to request transactions based on a specific year and month. This is how the API is structured:
routes.get('/transactions/:member_id/:year/:month', (req, res) => {
let {member_id, year, month} = req.params;
let start_date = new Date(year, month - 1, 1);
let end_date = moment(start_date).add(1, 'months');
console.log({start_date, end_date});
res.send({start_date, end_date})
});
To create this API, I am utilizing ExpressJS
for the building process and momentjs
for date manipulation.
When making a request to this API at http://localhost:8080/transactions/m/2017/10, I am asking for transactions belonging to user m
, within the year 2017
, and during the month 10
, which represents October (hence the adjustment of month - 1
in my implementation above).
The output displayed in the console is as follows:
{ start_date: 2017-09-30T11:00:00.000Z,
end_date: moment("2017-11-01T00:00:00.000") }
There appears to be confusion arising from the fact that the start day of the month has been set as 1
in the code snippet:
let start_date = new Date(year, month - 1, 1);
This raises the question of why it is not initiated as 30
instead. What could possibly be going wrong here?