Sequelize has two data types that you can use to put date-related values:
The DATEONLY type that converts to the SQL DATE format
The DATE type that converts to the SQL DATETIME format
const Invoice = sequelize.define(“Invoice”, {
invoiceDate: {
type: Sequelize.DATEONLY,
},
paymentDate: {
type: Sequelize.DATE,
},
amount: {
type: Sequelize.INTEGER,
},
},
{
timestamps: false,
});
—-+————-+———————+——–+
| id | invoiceDate | paymentDate | amount |
+—-+————-+———————+——–+
| 1 | 2022-01-17 | 2022-01-17 04:33:12 | 300 |
+—-+————-+———————+——–+
The sequelize.fn() method is used to call a native database function to modify the way your query works.
const invoices = await Invoice.findAll({
attributes: {
include: [
“id”,
“invoiceDate”,
[
sequelize.fn
(
“DATE_FORMAT”,
sequelize.col(“paymentDate”),
“%d-%m-%Y %H:%i:%s”
),
“paymentDate”,
],
“amount”,
],
},
});
console.log(invoices[0].toJSON());
output
{
id: 1,
invoiceDate: ‘2022-01-17’,
paymentDate: ’17-01-2022 04:33:12?,
amount: 300
}