colaf.svgColaf
login 

Author: colaf Date: 2024.03.10 01:46:24
Category: colaf
Subject: Date format in node.js, mariaDB, iphone shortcut
Content

Date format in node.js, mariaDB, iphone shortcut

COLAF 웹을 구현하면서 고민되는 것 중 하나는 date format이다. 아래와 같은 구조에서 date format이 서로 호환되지 않는다.

iphone   web(node.js)   DB(maria DB)

iphone에서 단축어를 사용해서 보낼 수 있는 date format은 다음과 같다.

2024-03-10T01:22:33.987+09:00

내가 다른 지역에 있다면? time zone이 바뀔테니까 +09:00 부분이 +01:00 이렇게 바뀌겠지

하지만 이 string을 그대로 maria DB에 저장하면 time zone부분은 무시되어 시간이 잘못 저장된다.

//In node.js
date='2024-03-10T01:22:33.987+09:00'

//In mariaDB
INSERT INTO `location`(`date`, `latitude`, `longitude`, `address`, `userId`, `displacement`) VALUES (date,?,?,?,?,?);
//Saved as 2024-03-10 01:22:33.987

node.js에서는 string을 받아서 time zone을 바꿔줄 수 있기 때문에 한국시간으로 바꿔서  maria DB에 저장하면 문제가 해결된다.

//In node.js
date='2024-03-10T01:22:33.987+09:00'
let date = new Date(date).toLocaleString('ja', {timeZone: 'Asia/Seoul', hour12: false});

maria DB에 저장되는 datetime format은 아래와 같다.

2024-03-10 01:23:33.987

maria DB에 저장되는 날짜는 한국을 기준으로 하기로 했다. 검색할 때도 편하고 mariaDB에서 node.js로 가져와서 time zone을 바꾸는 것도 어렵지 않기 때문이다.

 


tag: