2017년 12월 11일 월요일

Classic Asp date_format Function

언어별로 date_format 라는 함수가 있어 date값에 대해 원하는 string으로 출력해주는데 classic asp에는 없다.
그래서 다른 date_format 함수의 masking 방법을 빌려서 제작
format characterDescription
yyyy연도 4자리
yy연도 2자리
mm월 2자리
m월 1자리
dd일 2자리
d일 1자리
hh시 2자리
h시 1자리
ii분 2자리
i분 1자리
ss초 2자리
s초 1자리
WW요일
wex> 금

'/**
'**/
class DateValueObject
 private dateObj, formatValue
 
 public property let setDate(byval pDate)
  dateObj = cdate(pDate)
 end property
 
 public property get getDate()
  getDate = dateObj
 end property
 public property get getYear()
  getYear = year(dateObj)
 end property
 public property get getMonth()
  getMonth = month(dateObj)
 end property
 public property get getDay()
  getDay = day(dateObj)
 end property
 public property get getHour()
  getHour = hour(dateObj)
 end property
 public property get getMinute()
  getMinute = minute(dateObj)
 end property
 public property get getSecond()
  getSecond = second(dateObj)
 end property
 public property get getWeekDay()
  getWeekDay = Weekdayname(Weekday(dateObj)) 'left('목요일',1)
 end property
 public property get getWeekDaySimple()
  getWeekDaySimple = left(getWeekDay(),1) 'left('목요일',1)
 end property
 public property let setFormat(pFormat)
  formatValue = pFormat
 end property
 
 public property get getFormat()
  getFormat = formatValue
 end property
 private function zeroADD(byval pdate)
  dim vDate 
   vDate = pdate
   if len(vDate) = 1 then 
    zeroADD = "0" & vDate   
   else 
    zeroADD = vDate   
   end if 
 end function 
 public function dateParse()
  dim resultDate 
   resultDate = formatValue
   resultDate = replace(resultDate, "yyyy", getYear())    '연도(2017,1987)
   resultDate = replace(resultDate, "yy", right(getYear(),2))  '연도(2017,1987)
   resultDate = replace(resultDate, "mm", zeroADD(getMonth()))  '월(12,01,03)
   resultDate = replace(resultDate, "m", getMonth())    '월(12,1,3)
   resultDate = replace(resultDate, "dd", zeroADD(getDay()))  '일(12,01,03)
   resultDate = replace(resultDate, "d", getDay())     '일(12,1,3)
   resultDate = replace(resultDate, "hh", zeroADD(getHour()))  '시간(14,06,05)
   resultDate = replace(resultDate, "h", getHour())    '시간(14,6,5)
   resultDate = replace(resultDate, "ii", zeroADD(getMinute())) '분(14,06,05)
   resultDate = replace(resultDate, "i", getMinute())    '분(14,6,5)
   resultDate = replace(resultDate, "ss", zeroADD(getSecond())) '초(14,06,05)
   resultDate = replace(resultDate, "s", getSecond())    '초(14,6,5)
   resultDate = replace(resultDate, "W", getWeekDay())    '금요일
   resultDate = replace(resultDate, "w", getWeekDaySimple())  '금
   dateParse = resultDate
 end function 
 public function dateParseResult()
  dateParseResult = dateParse()
 end function 
end class
'/**
' @example   : date_format('2017-11-12 11:06:54','mm/dd/yyyy hh시 i분 s초') -> 11/12/2017 11시 6분 54초
'**/
function date_format(byval pdate, byval dateFormat)
 dim dateCl 
 set dateCl = new DateValueObject
  dateCl.setDate = pdate
  dateCl.setFormat = dateFormat
  
  date_format = dateCl.dateParseResult()
 set dateCl = nothing
end function