반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- spring3
- nodejs
- ajax
- Push
- rocky9
- jquery
- mybatis
- git
- MSsql
- PM2
- popup
- Maven
- pwa
- Java
- docker
- yona
- MariaDB
- mysql
- node.js
- Next.js
- security
- FCM
- NextJS
- Tomcat
- centos7
- SpringBoot
- config
- javascript
- jenkins
- Eclipse
Archives
- Today
- Total
ふたりで
nodejs OR nextjs 에서의 new Date 사용시 참고. 본문
728x90
반응형
SMALL
날짜 비교를 해서 기준 일시보다 클 경우 예외 처리를 하는 로직을 만들었다.
아래와 같다.
// 현재 날짜 및 시간 가져오기
const currentDateTime = new Date();
// 쿼리 실행
const selectResult = await pool.request()
.query(query);
const qrlimitdate = selectResult.recordset[0].di_qr_limit_date;
//날짜 및 시간 비교하여 체크후 제한시간보다 초과면 예외처리.
console.log("currentDateTime:",currentDateTime,",qrlimitdate:",qrlimitdate);
if(currentDateTime > qrlimitdate) {
throw new Error('제한시간을 초과 하였습니다.');
}
콘솔 로그로 날짜를 찍어보니 currentDateTime 현재시간이 아래와 같이 계속 이상 하게 찍히는 거다.
currentDateTime: 2024-09-04T05:58:38.133Z ,qrlimitdate: 2024-09-04T15:02:20.820Z
지금 시간은 한국 시간으로 15시인데 05시로 찍힌다.=_=;;
구글링 결과 nodejs Server 환경에서는 nodejs란 놈이 new Date로 시간을 가져오면 UTC기준으로 가져온다고....... 하;;;
아래는 해결한 소스 코드이다.
반응형
728x90
SMALL
// 현재 날짜 및 시간 가져오기
const currentDateTime = new Date();
//시간을 9시간 더해서 한국 시간으로 맞춰주자
currentDateTime.setHours(currentDateTime.getHours()+9);
// 쿼리 실행
const selectResult = await pool.request()
.query(query);
const qrlimitdate = selectResult.recordset[0].di_qr_limit_date;
//날짜 및 시간 비교하여 체크후 제한시간보다 초과면 예외처리.
console.log("currentDateTime:",currentDateTime,",qrlimitdate:",qrlimitdate);
if(currentDateTime > qrlimitdate) {
throw new Error('제한시간을 초과 하였습니다.');
}
콘솔 로그 확인 결과 아래와 같이 정상적으로 출력 및 비교도 된다.
currentDateTime: 2024-09-04T15:02:35.506Z ,qrlimitdate: 2024-09-04T15:02:20.820Z
nodejs 환경에서의 javascript는 이전에 클라이언트 브라우저 에서의 javascript처럼 그냥 대충 사용하지 말자;;;;;;
728x90
반응형
LIST
'node.js' 카테고리의 다른 글
pm2-logrotate Downgrade v2.7.0 to v2.6.0 (0) | 2024.04.17 |
---|---|
pm2 cluster + nextJS (0) | 2024.04.15 |
pm2 특정 app프로세스 강제 종료 및 초기화. (0) | 2024.03.19 |
pm2-logrotate (pm2 log 관리) (0) | 2024.03.13 |
Linux 또는 windows에서 node.js 용 MSSQL 접속 테스트 (0) | 2024.02.26 |
Comments