Home > Article > Backend Development > Time zone problem when saving Nodejs Date to mysql, how to deal with it
Mysql usage in nodejs
1. Establish a database connection: createConnection(Object) method This method accepts an object as a parameter. The object has four commonly used attributes: host, user, password, and database. The same parameters as the database link in php. The attribute list is as follows:
[javascript] view plaincopy
host: The host name where the database is connected. (Default: localhost)
port: The connection port. (Default: 3306)
localAddress: used for TCP connection IP address. (Optional)
socketPath: The path to the unix domain. This parameter will be ignored when using host and port.
user: The username of the MySQL user.
password: The password of the MySQL user.
database: The name of the database to link to (optional).
charset: Connection Character set. (Default: 'UTF8_GENERAL_CI'. Use uppercase when setting this value!)
timezone: The time zone where local time is stored. (Default: 'local')
stringifyObjects: Whether to serialize objects. See issue #501. (Default: 'false')
insecureAuth: Whether to allow old authentication methods to connect to the database instance. (Default: false)
typeCast: Determines whether column values are converted to native JavaScript type column values. (Default: true)
queryFormat: Custom query statement formatting function.
supportBigNumbers: When the database handles large numbers (long integers and decimals), it should be enabled (default: false).
bigNumberStrings: Enable supportBigNumbers and bigNumberStrings and force these Numbers are returned as strings (default: false).
dateStrings: Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings instead of a javascript Date object. (Default: false)
debug: whether Turn on debugging. (Default: false)
multipleStatements: Whether to allow multiple query statements to be passed in one query. (Default: false)
flags: Link flags.
Nodejs new Date() Save to mysql, Set the attribute dateStrings of the
connection to true, and add 8 hours to the saved time
var date = new Date();
date.setHours(date.getHours() + 8);
then save