imperium2335
imperium2335

Reputation: 24122

NodeJS dateFormat not working

When I retrieve a a column from my MySQL database (using mysql node module), I get a useless date formatted like:

Wed Sep 12 2012 08:44:26 GMT+0100 (GMT Daylight Time)

When I try to use the dateformat module on the returned result I simply get "Invalid date".

I want the date to be formatted the way MySQL formats it i.e. YYYY-MM-DD HH:MM:SS

Here is my script:

var lastChanged = '' ;
var lastUpdated = '' ;
var poll ;
function DBPoller()
{
poll = setTimeout(function(){
    connection.query('SELECT enquiryAdded FROM _table_updates', function(e, row) {
        if(e)
        {
            console.log('An error occured: '+e)
        }
        else
        {
            addedDate = dateFormat(row[0], 'yyyy-mm-dd hh:MM:ss') ;
            if(addedDate != lastChanged)
            {
                console.log('An enquiry was just added at '+addedDate+', the one before this was at '+lastChanged) ;
                lastChanged = addedDate ;
            }
        }
    });
    DBPoller() ;
}, 2000)
}

The above gives me the "Invalid date" error.

What am I missing?


Hi, the mysql column datatype is DATETIME.

Upvotes: 0

Views: 1603

Answers (1)

imperium2335
imperium2335

Reputation: 24122

I found it!

The result from MySQL is a JSON string:

{ enquiryAdded: Wed Sep 12 2012 08:44:26 GMT+0100 (GMT Daylight Time) }

So all I did was is access that value:

var addedDate = dateFormat(row[0].enquiryAdded, 'yyyy-mm-dd hh:MM:ss') ;

That's why I was getting "Invalid date"!

Upvotes: 1

Related Questions