지나간 시간 계산
현재로부터 몇시간 몇분 몇초전을 알려준다.
계산하는 스크립트 //
function transferTime(time){
var now = new Date();
var sYear = time.substring(0,4);
var sMonth = time.substring(4,6)-1;
var sDate = time.substring(6,8);
var sHour = time.substring(8,10);
var sMin = time.substring(10,12);
var sSecond = time.substring(12,14);
var sc = 1000;
var today = new Date(sYear,sMonth,sDate,sHour,sMin,sSecond);
//지나간 초
var pastSecond = parseInt((now-today)/sc,10);
var date;
var hour;
var min;
var str = "";
var restSecond = 0;
if(pastSecond > 86400){
date = parseInt(pastSecond / 86400,10);
restSecond = pastSecond % 86400;
str = date + "일 ";
if(restSecond > 3600){
hour = parseInt(restSecond / 3600,10);
restSecond = restSecond % 3600;
str = str + hour + "시간 ";
if(restSecond > 60){
min = parseInt(restSecond / 60,10);
restSecond = restSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = str + restSecond + "초 전";
}
}else if(restSecond > 60){
min = parseInt(restSecond / 60,10);
restSecond = restSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = str + restSecond + "초 전";
}
}else if(pastSecond > 3600){
hour = parseInt(pastSecond / 3600,10);
restSecond = pastSecond % 3600;
str = str + hour + "시간 ";
if(restSecond > 60){
min = parseInt(restSecond / 60,10);
restSecond = restSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = str + restSecond + "초 전";
}
}else if(pastSecond > 60){
min = parseInt(pastSecond / 60,10);
restSecond = pastSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = pastSecond + "초 전";
}
return str;
}
var now = new Date();
var sYear = time.substring(0,4);
var sMonth = time.substring(4,6)-1;
var sDate = time.substring(6,8);
var sHour = time.substring(8,10);
var sMin = time.substring(10,12);
var sSecond = time.substring(12,14);
var sc = 1000;
var today = new Date(sYear,sMonth,sDate,sHour,sMin,sSecond);
//지나간 초
var pastSecond = parseInt((now-today)/sc,10);
var date;
var hour;
var min;
var str = "";
var restSecond = 0;
if(pastSecond > 86400){
date = parseInt(pastSecond / 86400,10);
restSecond = pastSecond % 86400;
str = date + "일 ";
if(restSecond > 3600){
hour = parseInt(restSecond / 3600,10);
restSecond = restSecond % 3600;
str = str + hour + "시간 ";
if(restSecond > 60){
min = parseInt(restSecond / 60,10);
restSecond = restSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = str + restSecond + "초 전";
}
}else if(restSecond > 60){
min = parseInt(restSecond / 60,10);
restSecond = restSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = str + restSecond + "초 전";
}
}else if(pastSecond > 3600){
hour = parseInt(pastSecond / 3600,10);
restSecond = pastSecond % 3600;
str = str + hour + "시간 ";
if(restSecond > 60){
min = parseInt(restSecond / 60,10);
restSecond = restSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = str + restSecond + "초 전";
}
}else if(pastSecond > 60){
min = parseInt(pastSecond / 60,10);
restSecond = pastSecond % 60;
str = str + min + "분 " + restSecond + "초 전";
}else{
str = pastSecond + "초 전";
}
return str;
}
파라메터 time은 년월일시간분초(20120605112330)형식으론 넣어주여야 한다.
[출처] 자바스크립트 지난 시간 계산하기 |작성자 으잉
'IT노트 > javaScript' 카테고리의 다른 글
원하는 프레임만 프린트하기 (0) | 2015.03.01 |
---|---|
뒤로가기 버튼 javascript:history.back(-1) (0) | 2015.02.25 |
Ajax jsonp OR 서버측에서 크로스도메인 허용 방법 (0) | 2015.02.21 |
샘플 -basic _ popup.js (0) | 2015.02.20 |
[자바스크립트] 클로저(closure) (0) | 2015.02.15 |