1

I am using the mysql npm package with my Nodejs server. I have a datetime field that I want to display without time but I do not want to convert the data type to date

So I did

select cast(dateReached as date) as date 

Now if I run this in mysql workbench, I get the properly converted value. However, doing this using mysql package does not convert the datetime to time

pool.query("select cast(dateReached as date) as date",function(err,rows,fields){
   console.log(rows)
  //
})

Returns

RowDataPacket { Date: 2019-06-26T04:00:00.000Z }

Is there a way to cast date using the mysql package?

jedu
  • 1,211
  • 2
  • 25
  • 57

1 Answers1

2

It is returning only date, BUT it is being converted into Date object in Javascript which contains time.

You can,

  1. Return String from DB, using: dateStrings: true, Refer this gihub issue for more information.
    const db = mysql.createConnection({ user: 'foo', password: 'bar', dateStrings: true }); 
  1. Format the data After you get the Date object using the usual Date methods, like date's day / month / year. Refer this answer for more information.
Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35