Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Node.js (Express) and mySQL to INSERT for a TIMESTAMP?

Tags:

node.js

mysql

Just trying to do some simple messaging here:

   var post = {
        sender_username : sender_username,
        recipient_username : recipient_username, 
        message_no : 1,
        content : content, 
        time : ***CURRENT TIMESTAMP***, 
        read : 0
    };

    connection.query('INSERT INTO message SET ?', post, function(err, result) {
        if (err) {
            res.send(err);
        }
        else {
            res.send(post); 
        }
    });

What's the simplest way to stick the date and time in there that is valid for the TIMESTAMP type?

like image 437
senjougahara Avatar asked Jun 01 '14 07:06

senjougahara


1 Answers

You can use Moment.js for this purpose:

Date.now returns current timestamp in millisecond and moment also works with milliseconds.

var mysqlTimestamp = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
like image 58
Salar Avatar answered Oct 24 '22 23:10

Salar