Date 객체 사용예시
util.js 파일 생성해서 Date 객체를 통해 날짜 함수를 만들었다.
export function getToday() {
const aaa = new Date()
const yyyy = aaa.getFullYear()
const mm = aaa.getMonth() + 1
const dd = aaa.getDate();
const today = `${yyyy}-${mm}-${dd}`
return today
}
util.js파일의 getToday()함수를 import 해와서 사용해주는 모습이다.
import { getToday } from './util.js'
<div>가입일: ${getToday()}</div>
이렇게 출력이 되는데 2023-10-06이 되려면?
.toString().padStart(원하는 자릿수, 빈자리를 매꿀 문자)를 사용한다.
toString().padStart(자릿수, 문자)
const date = new Date();
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // getMonth()는 0부터 시작하므로 1을 더합니다.
console.log(month); // 예: "05"
아래처럼 적용해주니 잘 나온다.
export function getToday() {
const aaa = new Date()
const yyyy = aaa.getFullYear()
const mm = (aaa.getMonth() + 1).toString().padStart(2, "0");
const dd = aaa.getDate().toString().padStart(2, "0");
const today = `${yyyy}-${mm}-${dd}`
return today
}
'🪢node > node 이론 정리' 카테고리의 다른 글
rest-API || apollo-client (0) | 2023.10.09 |
---|---|
스프레드연산자 || 얕은/깊은복사 || REST 파라미터 (0) | 2023.10.08 |
구조분해할당+함수 선언 시 구조 분해 할당의 형태로 매개변수를 정의 하는 형태 (0) | 2023.10.06 |
파사드 패턴과 import 하기 (0) | 2023.10.05 |
우분투에 vscode 연동 (0) | 2023.10.04 |