Languages

node-postgres - difference in the date notation

0 points
Asked by:
Dirask Admin - MR
1340

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:

┌─────────┬──────────────────────────┬───────┐
│ (index) │          month           │ count │
├─────────┼──────────────────────────┼───────┤
│    0    │ 2021-02-28T23:00:00.000Z │  '5'  │
│    1    │ 2021-03-31T22:00:00.000Z │  '7'  │
└─────────┴──────────────────────────┴───────┘

My code: 

const { Client } = require('pg');

const client = new Client({
    host: '127.0.0.1',
    user: 'my_username',
    database: 'my_database',
    password: 'my_password',
    port: 5432,
});

const fetchEventsGroupedByMonths = async () => {
	const query = `
            SELECT
                DATE_TRUNC('month', "event_timestamp") AS "month",
                COUNT(*)
            FROM "events"
            GROUP BY DATE_TRUNC('month', "event_timestamp")
    `;
    await client.connect();
    try {
        const { rows } = await client.query(query);
        return rows;
    } finally {
        await client.end();
    }
};

fetchEventsGroupedByMonths()
    .then(result => console.table(result))
    .catch(error => console.error(error.stack));
0 answers
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join