본문 바로가기
nest.js

[nestjs] luxon 을 이용하여 심플하게 날짜 및 시간 포멧팅 처리 예제

by Hwoarang757 2025. 4. 4.

string type의 날짜 시간 형태의 문자열을 DateTime으로 변환 후 , 원하는 포멧으로 변경 하여 리턴 하는 예제 와 ,,, 

현재 일시에서 날짜 연산을 진행 하여 원하는 포멧으로 리턴 하는 예제 작성 해 보았습니다.

import { Logger } from "@nestjs/common";
import { DateTime } from "luxon";

export class CnvDateStrHelper {

  private static readonly logger = new Logger(CnvDateStrHelper.name);

  static convListDateTimeStr(sourceStr : string) : string {
    try {
        return DateTime.fromFormat(sourceStr , "yyyyMMddHHmss").toFormat("yyyy-MM-dd HH:mm:ss");
    } catch(e) {
        this.logger.error(e);
        return "";
    }
  }
  

  static getDaySpanStr(spanVal : number) : string {
    try {
      if(spanVal < 0)
        return DateTime.now().minus({ days : Math.abs(spanVal) }).toFormat('yyyyMMdd');
      else 
        return DateTime.now().plus({ days : Math.abs(spanVal) }).toFormat('yyyyMMdd');
    } catch(e) {
        this.logger.error(e);
        return "";
    }
  }