Convert SQL TIMESTAMP and DATE to Sequelize DATE and DATEONLY
See how SQL date and time types like TIMESTAMP, DATETIME, DATE, and TIME map to Sequelize DataTypes.DATE, DataTypes.DATEONLY, and DataTypes.TIME.
Detailed Explanation
Date and Time Types in Sequelize
SQL databases have several date/time column types, and Sequelize provides three primary DataTypes to handle them: DATE (datetime with timezone), DATEONLY (date without time), and TIME.
Mapping Table
| SQL Type | Sequelize DataType | JavaScript Type |
|---|---|---|
TIMESTAMP |
DataTypes.DATE |
Date |
TIMESTAMPTZ |
DataTypes.DATE |
Date |
DATETIME |
DataTypes.DATE |
Date |
DATE |
DataTypes.DATEONLY |
string (YYYY-MM-DD) |
TIME |
DataTypes.TIME |
string (HH:mm:ss) |
created_at and updated_at Handling
The converter detects common timestamp column names and configures Sequelize's built-in timestamp management:
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
The generated model includes timestamps: true with field name overrides:
{ timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at' }
CURRENT_TIMESTAMP Default
When a column has DEFAULT CURRENT_TIMESTAMP or DEFAULT NOW(), the converter maps it to defaultValue: DataTypes.NOW:
created_at: { type: DataTypes.DATE, defaultValue: DataTypes.NOW }
DATEONLY for Calendar Dates
SQL DATE (without time component) maps to DataTypes.DATEONLY, which returns a plain YYYY-MM-DD string in JavaScript rather than a full Date object. This is useful for birth dates, event dates, and other calendar-only values where time zone is irrelevant.
Use Case
You are building an event management system where events have both a calendar date (DATE) and precise timestamps for logging. Using the correct Sequelize DataType ensures proper serialization and timezone handling.
Try It — SQL to Sequelize Model
Related Topics
Convert a Simple SQL Table to a Sequelize Model
Basic Models
Convert SQL DEFAULT Values to Sequelize defaultValue
Advanced Features
Map SQL Integer Types to Sequelize DataTypes
Basic Models
Convert SQL JSON and JSONB to Sequelize DataTypes
Data Types
Convert SQL Primary Keys and Auto-Increment to Sequelize
Associations