유령노트
JS DATE에 FORMAT 프로토타입 추가 본문
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 29 30 31 32 33 34 35 36 37 38 | //date prototype 추가 Date.prototype.format = function(f) { if (!this.valueOf()) return " "; var weekName = ["일", "월", "화", "수", "목", "금", "토"]; var d = this; return f.replace(/(yyyy|yy|MM|dd|E|hh|mm|ss|ms|a\/p)/gi, function($1) { switch ($1) { case "yyyy": return d.getFullYear(); case "yy": return (d.getFullYear() % 1000).zf(2); case "MM": return (d.getMonth() + 1).zf(2); case "dd": return d.getDate().zf(2); case "E": return weekName[d.getDay()]; case "HH": return d.getHours().zf(2); case "hh": return ((h = d.getHours() % 12) ? h : 12).zf(2); case "mm": return d.getMinutes().zf(2); case "ss": return d.getSeconds().zf(2); case "ms": return d.getMilliseconds().zf(3); case "a/p": return d.getHours() < 12 ? "오전" : "오후"; default: return $1; } }); }; String.prototype.string = function(len){var s = '', i = 0; while (i++ < len) { s += this; } return s;}; String.prototype.zf = function(len){return "0".string(len - this.length) + this;}; Number.prototype.zf = function(len){return this.toString().zf(len);}; //사용방법 new Date("2021-10-11 11:22:33").format("yyyy-MM-dd HH:mm:ss"); <!-- 결과 --> 2021-10-11 11:22:33 | cs |
'# Dev > Javascript' 카테고리의 다른 글
카멜 표기법 변환용 JS (0) | 2022.04.12 |
---|---|
$(document).ready()를 javascript로 선언 (0) | 2022.02.21 |
undefined, null, 공백 체크 (0) | 2020.11.02 |
파일 체크 스크립트 (0) | 2020.09.24 |
JS에서 EXCEL -> CSV 변환 및 다운로드 기능 (0) | 2020.09.24 |