3

I'm using Node.js together with mysql, express and html.

When I'm retrieving the results from the database I get the following date, 2016-03-16T00:00:00.000Z, even though the type in mysql is date.

My query:

app.get('/list', function  (req,res) {
    connection.query('SELECT `ondate`,`projectname`,`hours` FROM `entries` WHERE YEARWEEK(`ondate`, 1) = YEARWEEK(CURDATE(), 1) ORDER BY ondate DESC ,projectname ASC ', function(err, rows, fields) {  
    res.json({error: err, result: rows});
    });
})

Is there a way I can set the ondate field to ignore the time?

1
  • Which mysql driver are you using ? Commented Mar 19, 2016 at 23:04

2 Answers 2

3

Use this:

SELECT
DATE_FORMAT(`ondate`,'%m-%d-%Y'),
`projectname`,
`hours` 
FROM `entries` 
WHERE YEARWEEK(`ondate`, 1) = YEARWEEK(CURDATE(), 1)
ORDER BY ondate DESC, projectname ASC;

Adjust the date format to the one you need.

Regards

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! This is what I needed
2

Hard to know exactly where the unwanted formatting is being introduced, but it might be an option to remove it as part of post processing rows...

// Assuming array of rows takes shape something like this...
var rows = [{ondate:'2012-01-02T00:00:00.000Z'},{ondate:'2013-01-02T00:00:00.000Z'},{ondate:'2012-03-02T00:00:00.000Z'}];

// map rows to show only first ten chars of `ondate` field
rows = rows.map(function(item){ return item.ondate.slice(0,10); });

// business as usual...
alert(rows);
//-> 2012-01-02,2013-01-02,2012-03-02

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.