• Home
  • About
    • lahuman photo

      lahuman

      열심히 사는 아저씨

    • Learn More
    • Facebook
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

Sequelize에서 날짜에 대한 타임존 처리 하기

01 Jun 2020

Reading time ~1 minute

Sequelize에서 날짜에 대한 타임존 처리 하기

Sequelize에서 날짜형(date)을 저장시 -9:00 된 값으로 저장이 됩니다.

EX) 6월 4일 00:00 => 6월 3일 15:00

서버의 타임존도 KST이고, DBMS의 타임존도 KST임에도 저장시 -9시간이 됩니다.

이럴때는 connection에 timezone을 설정하면 처리가 됩니다.

아래는 처리 하는 Sequelize Connection 예제입니다.

const Sequelize = require("sequelize");

// Option 1: Passing parameters separately
const sequelize = new Sequelize(
  process.env.MYSQL_DB,
  process.env.MYSQL_USER,
  process.env.MYSQL_PASS,
  {
    host: process.env.MYSQL_URL,
    dialect: "mysql",
    dialectOptions: { charset: "utf8mb4", dateStrings: true, typeCast: true }, // 날짜의 경우 문자열로 타입 변경 처리
    timezone: "+09:00", // 타임존을 설정
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000,
    },
    define: {
      // The `timestamps` field specify whether or not the `createdAt` and `updatedAt` fields will be created.
      // This was true by default, but now is false by default
      timestamps: false,
      supportBigNumbers: true,
    },
  }
);

module.exports = sequelize;

주요 설정으로 타임존과 날짜를 외부에 보낼때 처리만 추가 해주면 됩니다.

참고자료

  • [SEQUELIZE] TIMEZONE 세팅


sequelizetimezonemysql Share Tweet +1