so I am using MySQL as the database for my node app I can insert the date in YYYY-MM-DD
format but when I get data from it returns it as yyyy-mm-ddT00:00:00.000Z
so I want only the first part
db.query(`SELECT agereement_date FROM mt_A1 WHERE ledger_num = 15`,(err,data)=>{
console.log(data)
})
the output is like this
[
RowDataPacket {
agereement_date: 2021-03-07T18:30:00.000Z,
}
]
I Want only the YYYY-MM-DD
the first part I am using some JavaScript to rectify it but it feels unnecessary is there a way to get the date in that format directly from MySQL
2
Answers
Time in MySQL db is in without definition of timezone.
NodeJS converts time to
Date
object when You get it from server.So there are 3 solutions depending on Your use case:
if You don’t care timezone of browser: You can add
DATE_FORMAT
to sql query and take date part only.it’s still not timezone aware, but You can take date from db on backend and cut date part of it using:
as You can see I’ve defined in ISO format with
+02:00
so it may return differently depending on time of local computer.Modify your query :