메뉴 건너뛰기

조회 수 5704 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
/**
* 날짜관련 자바스크립트 공통함수
*
* 분단위 이하(= 초)는 고려하지 않았습니다.
* YYYYMMDDHHMI 형식의 String => 'Time'으로 칭함
*
* 주로 YYYYMMDD 까지만 쓰인다면 아래 함수들을
* YYYYMMDD 형식의 String => 'Date'로 하여 적당히
* 수정하시거나 아니면 함수를, 예를들어 isValidDate()처럼,
* 추가하시기 바랍니다.
*
* @version 2.0, 2001/01/28
* @author 박종진(JongJin Park), jongjpark@lgeds.lg.co.kr
*/


/**
* 유효한(존재하는) 월(月)인지 체크
*/
function isValidMonth(mm) {
   var m = parseInt(mm,10);
   return (m >= 1 && m <= 12);
}

/**
* 유효한(존재하는) 일(日)인지 체크
*/
function isValidDay(yyyy, mm, dd) {
   var m = parseInt(mm,10) - 1;
   var d = parseInt(dd,10);

   var end = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
   if ((yyyy % 4 == 0 && yyyy % 100 != 0) || yyyy % 400 == 0) {
       end[1] = 29;
   }

   return (d >= 1 && d <= end[m]);
}

/**
* 유효한(존재하는) 시(時)인지 체크
*/
function isValidHour(hh) {
   var h = parseInt(hh,10);
   return (h >= 1 && h <= 24);
}

/**
* 유효한(존재하는) 분(分)인지 체크
*/
function isValidMin(mi) {
   var m = parseInt(mi,10);
   return (m >= 1 && m <= 60);
}

/**
* Time 형식인지 체크(느슨한 체크)
*/
function isValidTimeFormat(time) {
   return (!isNaN(time) && time.length == 12);
}

/**
* 유효하는(존재하는) Time 인지 체크

* ex) var time = form.time.value; //'200102310000'
*     if (!isValidTime(time)) {
*         alert("올바른 날짜가 아닙니다.");
*     }
*/
function isValidTime(time) {
   var year  = time.substring(0,4);
   var month = time.substring(4,6);
   var day   = time.substring(6,8);
   var hour  = time.substring(8,10);
   var min   = time.substring(10,12);

   if (parseInt(year,10) >= 1900  && isValidMonth(month) &&
       isValidDay(year,month,day) && isValidHour(hour)   &&
       isValidMin(min)) {
       return true;
   }
   return false;
}

/**
* Time 스트링을 자바스크립트 Date 객체로 변환
* parameter time: Time 형식의 String
*/
function toTimeObject(time) { //parseTime(time)
   var year  = time.substr(0,4);
   var month = time.substr(4,2) - 1; // 1월=0,12월=11
   var day   = time.substr(6,2);
   var hour  = time.substr(8,2);
   var min   = time.substr(10,2);

   return new Date(year,month,day,hour,min);
}

/**
* 자바스크립트 Date 객체를 Time 스트링으로 변환
* parameter date: JavaScript Date Object
*/
function toTimeString(date) { //formatTime(date)
   var year  = date.getFullYear();
   var month = date.getMonth() + 1; // 1월=0,12월=11이므로 1 더함
   var day   = date.getDate();
   var hour  = date.getHours();
   var min   = date.getMinutes();

   if (("" + month).length == 1) { month = "0" + month; }
   if (("" + day).length   == 1) { day   = "0" + day;   }
   if (("" + hour).length  == 1) { hour  = "0" + hour;  }
   if (("" + min).length   == 1) { min   = "0" + min;   }

   return ("" + year + month + day + hour + min)
}

/**
* Time이 현재시각 이후(미래)인지 체크
*/
function isFutureTime(time) {
   return (toTimeObject(time) > new Date());
}

/**
* Time이 현재시각 이전(과거)인지 체크
*/
function isPastTime(time) {
   return (toTimeObject(time) < new Date());
}

/**
* 주어진 Time 과 y년 m월 d일 h시 차이나는 Time을 리턴

* ex) var time = form.time.value; //'20000101000'
*     alert(shiftTime(time,0,0,-100,0));
*     => 2000/01/01 00:00 으로부터 100일 전 Time
*/
function shiftTime(time,y,m,d,h) { //moveTime(time,y,m,d,h)
   var date = toTimeObject(time);

   date.setFullYear(date.getFullYear() + y); //y년을 더함
   date.setMonth(date.getMonth() + m);       //m월을 더함
   date.setDate(date.getDate() + d);         //d일을 더함
   date.setHours(date.getHours() + h);       //h시를 더함

   return toTimeString(date);
}

/**
* 두 Time이 몇 개월 차이나는지 구함

* time1이 time2보다 크면(미래면) minus(-)
*/
function getMonthInterval(time1,time2) { //measureMonthInterval(time1,time2)
   var date1 = toTimeObject(time1);
   var date2 = toTimeObject(time2);

   var years  = date2.getFullYear() - date1.getFullYear();
   var months = date2.getMonth() - date1.getMonth();
   var days   = date2.getDate() - date1.getDate();

   return (years * 12 + months + (days >= 0 ? 0 : -1) );
}

/**
* 두 Time이 며칠 차이나는지 구함
* time1이 time2보다 크면(미래면) minus(-)
*/
function getDayInterval(time1,time2) {
   var date1 = toTimeObject(time1);
   var date2 = toTimeObject(time2);
   var day   = 1000 * 3600 * 24; //24시간

   return parseInt((date2 - date1) / day, 10);
}

/**
* 두 Time이 몇 시간 차이나는지 구함

* time1이 time2보다 크면(미래면) minus(-)
*/
function getHourInterval(time1,time2) {
   var date1 = toTimeObject(time1);
   var date2 = toTimeObject(time2);
   var hour  = 1000 * 3600; //1시간

   return parseInt((date2 - date1) / hour, 10);
}

/**
* 현재 시각을 Time 형식으로 리턴

*/
function getCurrentTime() {
   return toTimeString(new Date());
}

/**
* 현재 시각과 y년 m월 d일 h시 차이나는 Time을 리턴
*/
function getRelativeTime(y,m,d,h) {
/*
   var date = new Date();

   date.setFullYear(date.getFullYear() + y); //y년을 더함
   date.setMonth(date.getMonth() + m);       //m월을 더함
   date.setDate(date.getDate() + d);         //d일을 더함
   date.setHours(date.getHours() + h);       //h시를 더함

   return toTimeString(date);
*/
   return shiftTime(getCurrentTime(),y,m,d,h);
}

/**
* 현재 年을 YYYY형식으로 리턴
*/
function getYear() {
/*
   var now = new Date();
   return now.getFullYear();
*/
   return getCurrentTime().substr(0,4);
}

/**
* 현재 月을 MM형식으로 리턴
*/
function getMonth() {
/*
   var now = new Date();

   var month = now.getMonth() + 1; // 1월=0,12월=11이므로 1 더함
   if (("" + month).length == 1) { month = "0" + month; }

   return month;
*/
   return getCurrentTime().substr(4,2);
}

/**
* 현재 日을 DD형식으로 리턴

*/
function getDay() {
/*
   var now = new Date();

   var day = now.getDate();
   if (("" + day).length == 1) { day = "0" + day; }

   return day;
*/
   return getCurrentTime().substr(6,2);
}

/**
* 현재 時를 HH형식으로 리턴
*/
function getHour() {
/*
   var now = new Date();

   var hour = now.getHours();
   if (("" + hour).length == 1) { hour = "0" + hour; }

   return hour;
*/
   return getCurrentTime().substr(8,2);
}

/**
* 오늘이 무슨 요일이야?

* ex) alert('오늘은 ' + getDayOfWeek() + '요일입니다.');
* 특정 날짜의 요일을 구하려면? => 여러분이 직접 만들어 보세요.
*/
function getDayOfWeek() {
   var now = new Date();

   var day = now.getDay(); //일요일=0,월요일=1,...,토요일=6
   var week = new Array('일','월','화','수','목','금','토');

   return week[day];
}

List of Articles
번호 제목 날짜 조회 수
247 바닐라 JS, ECMAScript 개념 file 2023.01.20 109
246 마우스 오른쪽버튼 , 드래그 선택 차단 2023.01.12 118
245 패스워드, 확인패스워드가 맞는지 체크 2023.01.12 130
244 Node.js와 npm(+ npx)의 개념 2023.01.20 134
243 비동기 작업의 원리 (JavaScript 엔진, Web API, Task Queue, Event Loop) file 2023.01.20 138
242 개발자도구 F12키 막기 file 2023.01.12 176
241 페이지 이동 2021.03.26 190
240 getYear(); 크롬, 파이어폭스 에서 제대로 작동 안하는 문제 2021.03.26 197
239 default 매개변수(매개변수 기본값) 2021.03.26 199
238 자주쓰는 것들 2021.03.26 204
237 split, join, replace, replace_all 2021.03.26 214
236 폼안에 태그명, 함수명 같을때 오류 2021.03.26 215
235 현재 날짜, 시간 ( Month + 1 에 대해서 ) 2021.03.25 218
234 Date 객체로 원하는 날짜, 시간 표현하기 2021.08.20 223
233 3자리 마다 쉼표만 찍어주는 number_format 함수 2021.03.26 225
232 Javascript - 입력한 년, 월의 마지막 날짜 구하기 2021.03.09 226
231 월의 마지막 날짜 계산하기 2021.08.20 242
230 오브젝트 속성 2021.03.25 244
229 대소문자 변경 (대문자를 소문자로, 소문자를 대문자로) 2021.08.20 261
228 Javascript - form태그 내부 ajax처리시 2번 전송되는 현상 2021.03.09 269
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved