mysql - Select query returns different timezone for datetime column -
i using nodejs , express framework , mysql database
i have set default-time-zone in [mysqld] section in file
c:\programdata\mysql\mysql server 5.7\my.ini
as follows
default-time-zone='+05:00'
here global time zone , session time zone
the column name registration_date .. datetime column
here result of select statement in mysql workbench
i using database query in function
getparentrecord: function (mobilenumber, callback) { db.query('select parent_id , name , cnic ,mobilenumber ,email ,parent_type ,convert_tz(registration_date,\'+05:00\',@@global.time_zone) registration_date,is_registered,is_active,source_id parentslist mobilenumber=?', mobilenumber, function (err, result) { if (err) { var parent_record_dberror = { status: "fail", message: "there problem connecting our website please try again" } callback(parent_record_dberror, null); } else if (result.length < 1) { var parent_record_notfounderror = { status: "fail", message: "this user not in our database .. please register first" } callback(parent_record_notfounderror, null); } else if (result.length !== 0) { callback(null, result); } else { } }); }
then in route file calling function follows
parent.getparentrecord(parentmobilenumber, function(err, updatedparentrecordfromdb) { //obtain parent record database if (err) { res.json(err); } else { if (err) { res.json(err); } else { var updatedparentrecord = { parent_id: updatedparentrecordfromdb[0].parent_id, name: updatedparentrecordfromdb[0].name, cnic: updatedparentrecordfromdb[0].cnic, mobilenumber: updatedparentrecordfromdb[0].mobilenumber, email: updatedparentrecordfromdb[0].email, parent_type: updatedparentrecordfromdb[0].parent_type, registration_date: updatedparentrecordfromdb[0].registration_date, is_registered: updatedparentrecordfromdb[0].is_registered, is_active: updatedparentrecordfromdb[0].is_active, source_id: updatedparentrecordfromdb[0].source_id }; res.status(200).send(updatedparentrecord); } } });
so have set timezone utc+5 select query still returning utc format after use convert_tz function i.e
'select parent_id , name , cnic ,mobilenumber ,email ,parent_type ,convert_tz(registration_date,\'+05:00\',@@global.time_zone) registration_date,is_registered,is_active,source_id parentslist mobilenumber=?'
in mysql workbench displays datetime in current timezone .. not in nodejs code ..
so how display datetime in current timezone format ?
the query returned object ended using function tolocalestring() display datetime in local timezone
https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/date/tolocalestring
Comments
Post a Comment