Query in Sequelize separating Month and Year

Asked

Viewed 211 times

0

Hi, guys. Sorry if my question seems a bit silly, but I’m starting now and doing this query is tying my head in a knot kkk

Next, I’m building a system to manage the company’s vouchers (advances). I already created the login system using express-Session and it’s working fine. Only now I need to create a page that the person sees all their vouchers in the current month. For example: If I entered today (15/08/2020), I would see all the vouchers I requested in August 2020. Do the query looking for the corresponding user id valleys I already got quiet as follows:

const finantialController = {
financeiro: async (req,res)=>{
    try {
        const vales = await Vales.findAll({
            where:
                {
                userId: req.session.user.id,
                },
            include: [{model:User}]
        })
        res.render('finantial/financeiro', {vales: vales})

This piece of code is working beautifully, but I don’t know how to store the current date (maybe I’ll have to do this through datetime) and how to separate the month and year from my database and compare it with datetime. I already have the timestamps in the database, so I would use the "createdAt" column. I have read the documentation, I have tried some ways, I have done a lot of research but unfortunately I have not reached any solution. If you can help me I really appreciate!

1 answer

0


It’s a bit confusing but I’ll try to help! the variable valleys must return an array with columns referring to the given ID, certain?!

to pick the month and year you can use the Date by passing the value of createAt:

const date = new Date("2020-05-06 05:20:30")
console.log(date.getMonth(), date.getYear())

Same thing to catch the current year and month, just remove the "2020-05-06 05:20:30"

Done this you will get the table and current dates. then you can make a comparison by traversing the array with a MAP and comparing the values with IF.

  • 1

    Man, thank you so much! You gave me a great idea and I managed to solve doing the following: I created a TODAY variable that gets today’s date, then created a MONTH that gets Today.gethMonth(), then another year call that gets Today.getYear(). Then I created a function (SAMEMONTHANDYEAR) that first filters by month, then by year and returns this value. And then I took that array there VALES and applied a filter with the function. It was right. Thank you very much, man!

  • Arrange! if you can mark as useful answer I would be happy

Browser other questions tagged

You are not signed in. Login or sign up in order to post.