Search code examples
mysqlnode.jsheroku

Nodejs mysql timezone issue


I am using the node mysql library.

In one of my Insert Queries I used now() to insert the current time. However, I am seeing a different time than the one in my time zone. I am using the cleardb from heroku as my mysql database. I did not find any settings there to change the time zone. Is is a bad practice to use now()? Is there a javascript alternative that I can use to get the current date and time that is similar to now()?


Solution

  • You can use new Date(); in Javascript or something similar get the current date/time in UTC and store that UTC format dateTime to the database.

    After you query the date (ex: select Date from table1;) and return it to the front end, you can use .toLocaleString(); to convert it to "normal" dateTime string. and you can even further use the .toLocaleString options to display the dateTime in the format that you want, check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString