sqlite 使用sequelize比较node js中的日期

xn1cxnb4  于 2023-04-21  发布在  SQLite
关注(0)|答案(2)|浏览(143)

实际上,我正在使用sqlite数据库(表). Node.js作为后端。现在我需要获取两个特定日期下的记录。(例如24.04.2020 11.00 AM到28.04.2020 02.00 pm)我如何做到这一点?我需要以什么格式在数据库中存储日期和时间?以及我如何比较?

v9tzhpje

v9tzhpje1#

Sequelize使用js date。所以你可以这样做:

const { Op } = require("sequelize");

Foo.findAll({
  where: {

    // myDateColumn < [timestamp] AND myDateColumn > [timestamp]
    {
      myDateColumn: {
        [Op.lt]: new Date(),
        [Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000)
      }
    },

    // myDateColumn in range
    [Op.notBetween]: [new Date(), new Date(new Date() - 24 * 60 * 60 * 1000)]
  }
});

我还建议使用moment.js或luxon(个人最喜欢的)来处理日期。
使用luxon格式化日期:

const DateTime = require('luxon');

// the reason we love luxon is because it's easy to set the timezone 
const startDate = DateTime.local().setZone('America/New_York').fromISO('2020-04-24T11:00:00').toJSDate();

阅读更多:https://sequelize.org/master/manual/model-querying-basics.html

bfnvny8b

bfnvny8b2#

const currentDate = new Date().toISOString().slice(0,10);//获取ISO格式的当前日期try { const competitions = await CompetitionTable.findAll();

const competitionsWithActiveFlag = competitions.map(competition => {
  const isActive = (
    competition.start_date <= currentDate &&
    competition.end_date >= currentDate
  );

  const competitionData = competition.toJSON();
  console.log(competitionData, isActive);
 
});

相关问题