EN
node-postgres - difference in the date notation
0 answers
0 points
I wanted to group the rows in my database by day, but when I query heidiSQL and then node.js, I get a difference in the results - the days are different in both cases. How to fix it?
Results in HeidiSQL:
Results in Node.js:
xxxxxxxxxx
1
┌─────────┬──────────────────────────┬───────┐
2
│ (index) │ month │ count │
3
├─────────┼──────────────────────────┼───────┤
4
│ 0 │ 2021-02-28T23:00:00.000Z │ '5' │
5
│ 1 │ 2021-03-31T22:00:00.000Z │ '7' │
6
└─────────┴──────────────────────────┴───────┘
My code:
xxxxxxxxxx
1
const { Client } = require('pg');
2
3
const client = new Client({
4
host: '127.0.0.1',
5
user: 'my_username',
6
database: 'my_database',
7
password: 'my_password',
8
port: 5432,
9
});
10
11
const fetchEventsGroupedByMonths = async () => {
12
const query = `
13
SELECT
14
DATE_TRUNC('month', "event_timestamp") AS "month",
15
COUNT(*)
16
FROM "events"
17
GROUP BY DATE_TRUNC('month', "event_timestamp")
18
`;
19
await client.connect();
20
try {
21
const { rows } = await client.query(query);
22
return rows;
23
} finally {
24
await client.end();
25
}
26
};
27
28
fetchEventsGroupedByMonths()
29
.then(result => console.table(result))
30
.catch(error => console.error(error.stack));
0 answers