0

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

6
  • When it comes to timestamps you would be better using UNIX Timestamp. Also make sure to have everything in UTC time Commented Jan 3, 2019 at 11:01
  • I got all my timestamp with UTC Commented Jan 3, 2019 at 11:03
  • Use UNIX Timestamp for any time related task Commented Jan 3, 2019 at 11:03
  • Can you give me a complete answer?😥 Commented Jan 3, 2019 at 11:06
  • Why are you getting the current time using Node.js? Can't you get it from the database as suggested by @Pe46dro? Commented Jan 3, 2019 at 13:12

3 Answers 3

2

You are comparing two strings. Try replacing this :

            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) {

with this :

            var rowResult = JSON.parse(JSON.stringify(rows[0]));
            var nodeTime = Date.now();
            var mysqlTime = new Date(rowResult.last_pub).getTime();
            if (mysqlTime > nodetime) {
Sign up to request clarification or add additional context in comments.

Comments

1

I think you should generate timestamp in MySQL with the function unix_timestamp. Then you should use MySQL perform the comparison and retrive only the lines that you need.

Functions:

Comments

0

You can either use moment functions isSameOrBefore or isSameOrAfter:

var moment1 = moment(rowResult.last_pub);
var moment2 = moment(); // Current datetime

moment1.isSameOrAfter(moment2);

moment1.isSameOrBefore(moment2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.