Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Date different while retrieving from node.js

I want to query the records while their insertion date is between two dates and that is like this:

SELECT `products`.`Product_Name`, `products`.`Product_Type`, `products`.`Product_Date` Where 
`products`.`Product_Date` between CONCAT(YEAR(CURRENT_DATE),"- 
 ",MONTH(CURRENT_DATE),"-","01") and (LAST_DAY(CURRENT_DATE))

the result in MySql Is like this:

In MySQL result

but in the server-side (I use Node.js) the result is different and that is like this

[
   {"Product_Name":"Water","Product_Type":"Large","Product_Date":"2018-03-24T18:30:00.000Z"},
    {"Product_Name":"Water","Product_Type":"Small","Product_Date":"2018-03-24T18:30:00.000Z"},
    {"Product_Name":"Water","Product_Type":"Large","Product_Date":"2018-02-28T18:30:00.000Z"},
    {"Product_Name":"Water","Product_Type":"Large","Product_Date":"2018-03-30T18:30:00.000Z"}
] 

I want to query the inserted products in the current month from 1st till last date of current month, I get the right result in mysql but wrong result in the server node js, in the third object it shows also the previous month date too. not only that, even the format of the date is also different in the server side

like image 546
m.o Avatar asked Oct 24 '25 18:10

m.o


1 Answers

This looks like a timezone issue. The node dates are using a standard format (ISO 8601) for representing date/time. The Z at the end indicates the time is in UTC time. Mysql is showing you the time in local time. See https://dev.mysql.com/doc/refman/5.7/en/datetime.html. Local time can mean either mysql server timezone or timezone for your connection (might be the same), depending on how you setup your connection. My guess, from the above output, is that you are in a timezone that is at least 5 and half hours ahead of UTC time. UTC+5:30 would be India or Sri Lanka. So maybe you are in one of those places, or to the east of there. Some options, in your node mysql connection you can set the timezone. E.g., see the connection options here: https://www.npmjs.com/package/mysql#establishing-connections

like image 137
Mike Atkins Avatar answered Oct 26 '25 07:10

Mike Atkins