Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Js sequelize select query by month

Am new in Node Js, In my Node Js project am using sequelize ORM with MySql database.

This is my query i want to write select query by month.

This is my query SELECT * FROM cubbersclosure WHERE MONTH(fromDate) = '04'

Here fromDate field type is date

enter image description here

This my code:

var fromDate = '2019-04-01'
var fromDateMonth = new Date(fromDate);
var fromMonth = (fromDateMonth.getMonth()+ 1) < 10 ? '0' + (fromDateMonth.getMonth()+1) : (fromDateMonth.getMonth()+1);

CubbersClosure.findAll({
    where:{
        // select query with Month (04)... //fromMonth
    }
}).then(closureData=>{        
    res.send(closureData);
}).catch(error=>{
    res.status(403).send({status: 'error', resCode:200, msg:'Internal Server Error...!', data:error});
});

Here fromMonth get only month from date, so i want to write code select query by month.

like image 741
Ramesh S Avatar asked May 24 '26 03:05

Ramesh S


1 Answers

I'm not sure but what about try this?

where: {
  sequelize.where(sequelize.fn("month", sequelize.col("fromDate")), fromMonth)
}
like image 71
kkangil Avatar answered May 26 '26 09:05

kkangil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!