I am trying to compare timestamp of MySQL with current timestamp on NodeJS.
I am adding the timestamp in this way to MySQL:
var updateQuery = "UPDATE user SET last_pub = ? + INTERVAL 5 HOUR WHERE uid = '" + uid + "'";
var mysqlTimestamp = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
return req.dbConnection.query(updateQuery, mysqlTimestamp) // I call this to run the query
I compare it in this way:
function checkTime() {
return req.dbConnection.query(checkUser)
.then(([rows, fields]) => {
if (rows != "") {
var rowResult = JSON.parse(JSON.stringify(rows[0]));
var mysqlTimestamp = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
console.log(rowResult.last_pub)
console.log(mysqlTimestamp)
if (rowResult.last_pub > mysqlTimestamp) {
var dict = {}
dict.response = "wait"
console.log("hour not passed");
req.dbConnection.end()
console.log(JSON.stringify({"status": 200, "error": null, "response": dict}))
return res.send(JSON.stringify({"status": 200, "error": null, "response": dict}));
}
else {
console.log("done")
}
}
else {
console.log("Error - user not exist")
}
}).catch((error) => console.log(error));
}
When I try to print the two timestamp I got this two:
2019-01-03T04:02:00.000Z (MySQL result)
2019-01-03 00:02:07 +01:00 (moment result)
The problem is that the hour is everytimes "hour not passed" even if the last_pub is < of mysqlTimestamp
I have done some test and when I do the check with rowResult.last_pub > mysqlTimestamp its not checking the hour but its checking the day. Because If I change the hour its always "hour not passed" but If I change the day it print "done". I have to do the check with hour too
N. = All my timestamp is in UTC (System and MySQL) and the field’s type of time where we are working is set to TIMESTAMP