nubiz님의 스크립트🔗를 일부 수정해 사용
힘들다 힘들어😩🤯
🔗 소스코드
- 방금 전/n분/시간/일/주/개월 전 형식
- 1년이 지난 게시글은 날짜가 그대로 나타남
c1에 적용할 클래스명을 넣고 메서드를 호출하면 된다.
function modDate(cl) {
$("."+cl).each(function(){
if(!$(this).is(".modDate")) {
var dStr,YYYY,MM,DD,hh,mm,date,now,dateMs,nowMs,readableDate,r,error;
dStr= $(this).text().trim();
date = new Date();
now = new Date();
switch(dStr.length) {
case 4:
r=dStr;
date.setFullYear(r);
break;
case 5:
if (dStr.match(":")) {
hh=dStr.substring(0,2);
mm=dStr.substring(3,5);
date.setHours(hh,mm);
} else if(dStr.match(".")) {
MM=dStr.substring(0,2);
DD=dStr.substring(3,5);
date.setMonth(MM-1,DD);
}
break;
case 8:
hh=dStr.substring(0,2);
mm=dStr.substring(3,5);
date.setHours(hh,mm);
break;
case 10:
YYYY= dStr.substring(0,4);
MM= dStr.substring(5,7);
DD= dStr.substring(8,10);
date.setFullYear(YYYY,MM-1,DD);
break;
case 16:
YYYY= dStr.substring(0,4);
MM= dStr.substring(5,7);
DD= dStr.substring(8,10);
hh= dStr.substring(11,13);
mm= dStr.substring(14,16);
date.setFullYear(YYYY,MM-1,DD);
date.setHours(hh,mm);
break;
default:
error=true;
break;
}
if(!error) {
if(hh) {
readableDate = date.getFullYear()+"."+(date.getMonth()+1)+"."+date.getDate()+" "+date.getHours()+":"+date.getMinutes();
} else if(MM) {
readableDate = date.getFullYear()+"."+(date.getMonth()+1)+"."+date.getDate();
} else {
readableDate=date.getFullYear();
}
dateMs = date.getTime();
nowMs = now.getTime();
r = nowMs-dateMs;
r = r/1000;
if(hh) {
if(r<240) {
$(this).text("방금 전");
} else if(r < 3600) {
r=Math.round(r/60);
$(this).text(r+"분 전");
} else if(r < 86400) {
r = Math.round(r/3600);
$(this).text(r+"시간 전");
}
} else {
if(r<86400) {
r = readableDate;
$(this).text("오늘");
}
}
if (r>=86400) {
r=r-now.getHours()*60*60;
r=Math.ceil(r/86400);//sec to days
if (r<7){
$(this).text(r+"일 전");
} else if (r<60) {
r=Math.round(r/7);
$(this).text(r+"주 전");
} else if(r<305) {
r=Math.round(r/30.5);
$(this).text(r+"개월 전");
} else {
r=readableDate;
$(this).text(r);
}
}
}
}
});
}
댓글의 경우 텍스트에 날짜뿐만 아니라 신고를 위한 <a>가 포함되어 있어 스크립트 적용이 안된다.
따라서 신고 태그를 날짜 태그 밖으로 빼주는 스크립트를 미리 작성하였다.
window.onload = function() {
$(".comment-date a").each(function() {
$(this).addClass("report");
$(this).parent().parent().find(".control").prepend($(this));
$(this).text("🤐");
});
}
이유는 모르겠는데 댓글이 대체 뭔지 스크립트 코드가 잘 안먹는다🤦♀️
이모지도 적용이 됐다 안됐다 하고 ready()도 안먹어서 결국 load()로 작성함
⌛️ 결과
댓글 바뀌냐?