본문 바로가기

html | javascript

html javascript - date string format

function getDateStr00(date) {
  return date.toLocaleString()
}

function getDateStr00_01(date) {
  return date.toLocaleDateString()
}

function getDateStr00_02(date) {
  return date.toLocaleTimeString()
}

function getDateStr01(date) {
  const y = date.getFullYear()
  const m = date.getMonth() + 1
  const d = date.getDate()
  const h = date.getHours()
  const mm = date.getMinutes()
  const s = date.getSeconds()
  const mStr = (m < 10)? `0${m}`:`${m}`
  return `${y}-${mStr}-${d} ${h}:${mm}:${s}`
}

function getDateStr01_01(date) {
  const y = date.getFullYear()
  const m = date.getMonth() + 1
  const d = date.getDate()
  const mStr = (m < 10)? `0${m}`:`${m}`
  return `${y}-${mStr}-${d} ${date.toLocaleTimeString()}`
 }

function getDateStr02(date) {
  const y = date.getFullYear()
  const m = date.getMonth() + 1
  const d = date.getDate()
  const h = date.getHours()
  const mm = date.getMinutes()
  const s = date.getSeconds()
  const mStr = (m < 10)? `0${m}`:`${m}`
  return `${y}년 ${mStr}월 ${d}일 ${h}시 ${mm}분 ${s}초`
}

function getDateStr03(date) {
  const y = date.getFullYear()
  const m = date.getMonth() + 1
  const d = date.getDate()
  let h = date.getHours()
  const mm = date.getMinutes()
  const s = date.getSeconds()
  const mStr = (m < 10)? `0${m}`:`${m}`
  const amPm = (h >= 12) ? '오후':'오전'
  h = h % 12
  if (h === 0) h = 12
  return `${y}년 ${mStr}월 ${d}일 ${amPm} ${h}시 ${mm}분 ${s}초`
}