Recent posts

Recent comments

Archive

Calender

«   2024/04   »
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


티스토리에서,
글을 "공개"하거나, "발행"했을 때
그 글의 마지막 수정된 날짜를 가져오는 방법이다.


<meta property="article:modified_time" content=""> 이라는 태그를 이용하는 방법이다.


<meta> 태그의 "property" 속성값이 "article:modified_time"이라면,
그 <meta> 태그의 "content" 속성에 저장된 숫자를 가져와서,
자바스크립트의 "new Date()" 함수를 이용하여, 그 숫자들을 날짜 형식으로 바꿔준다.



참고로, "비공개" 글에는, 위의 메타 태그가 들어있지 않았다.



<meta> 태그에 들어있는 property="article:modified_time"의 자세한 의미는 아래 링크를 참고하기 바란다.
The Open Graph protocol (영문)



 
<script type="text/javascript">

// 출처 : http://tonks.tistory.com/122
// 에러가 나온다면, 여기에 댓글을 남겨주세요. 

function getModifiedDate (){ 

    var metaTags = document.getElementsByTagName( "META" ); 

    var contentValue = "", x, getAttr; 

    for ( x = 0; x < metaTags.length; x++ ){ 

            // <meta> 태그 안에 들어있는 "property"이라는 속성을 가져오기. 
            getAttr = metaTags[x].getAttribute( "property" ); 

            // "property"의 속성값이 "article:modified_time"이 아니라면 건너뛰기. 
            if ( getAttr != "article:modified_time" ){  continue;  } 

            else {  contentValue = metaTags[x].getAttribute( "content" );  } 
    } 

    // "contentValue"의 값이 바뀌지 않았다면, 빈 문자( "" )만 남기고, 여기서 끝내기. 
    if ( contentValue === "" ){  return contentValue;  } 

    var array = contentValue.split( "T" ); 
    var date = array[ 0 ].replace( /\-/g , "/" ); 
    var time = array[ 1 ].split( "+" )[ 0 ]; 

    return new Date( date + " " + time ); 
} 
</script>
 


 
<p id="demo">  </p>

<script type="text/javascript">

document.getElementById( "demo" ).innerHTML = getModifiedDate(); 

</script>
 


 
<button onclick="testing()"> 클릭하세요 </button>

<p id="date"> </p>
<p id="time"> </p>

<script type="text/javascript">

function testing () { 

      var getDate = "", getTime = ""; 
      var modify = getModifiedDate(); 

      if ( modify.toString().length > 0 ) { 

            getDate = modify.toLocaleDateString(); 
            getTime = modify.toLocaleTimeString(); 
      } 

      document.getElementById( "date" ).innerHTML = getDate; 
      document.getElementById( "time" ).innerHTML = getTime; 
} 
</script>
 



이 내용이 도움이 되셨다면, 아래 버튼을 눌러주세요. *^^*