Best way to process data and group it by month day and hour

I am pulling the data from the external API and specially interested in how to process datetime data. So if my datetime is 02/28/2923 12:35:35 what is best way to group this data by the hour ?

What data format is this? A JavaScript date or a string?

Also define ‘group this data’.

Some example input and expected output would be helpful.


The details requested others would help. If the following is not relevant then please provide clarification.

You could use the datetime values with the minutes, seconds and milliseconds set to zero.


As an example If I have array of 100 datetime string which represents the timestamp the call was made. I would like to count this and show a table that might look like this.

9am - 52 calls
10am - 8 calls
11am - 10 calls
12pm - 10 calls
1pm - 15 calls
2pm - 5 calls

let calls = {};
callsFromAPI.forEach((x) => calls[x.substring(0,13)] = (calls[x.substring(0,13)] ?? 0) + 1 );
1 Like

why are you taking date part instead of the time part?

Well if you have calls from 9 AM on Monday, and 9 AM on Tuesday, do you want them lumped together?

Substring 13 will capture the day, month, year, and hour.

02/28/2923 12:35:35
Substring 13 ends here

If you DO want to put the calls from Monday and Tuesday in the same buckets, and are doing grouping PURELY by hour, change the 0 in the substring to 11. Voila. Capturing only the hour.

Is the data arriving

  • A: one call at a time

  • B: all calls each hour

  • C all calls for the day

If A Have an array indexed by hours initialised to zero and increment the appropriate index for each data item received. If you want to keep the data then make it an array of arrays and push the data onto the correct hour array.
For B and C do the same while iterating the array, and display the hour array lengths.

Yes they will be grouped together

What do you mean change the 0 in the substring to 11?

that is a good point. This API works from by giving range of dates (start and end date). As of now I am thinking about calling start day to be be first day of the current month and end day to be current date. this is going to give me all the calls from 1st until now

                                               ^                           ^
                                               The zeroes. In the substring.