2014년 3월 19일 수요일

[javascript] Date Control

To get year - should use getFullYear() instead getYear()
To get month - should be add '1' (getMonth() is array. array starts from '0')

-- HTML

<div>
    <table><tbody>
            <tr>
                <th>today</th><td id="today"></td>
            </tr>
            <tr>
                <th>year</th><td id="year"></td>
            </tr>
            <tr>
                <th>fullYear</th><td id="fullYear"></td>
            </tr>
            <tr>
                <th>month</th><td id="month"></td>
            </tr>
            <tr>
                <th>month_add</th><td id="month_add"></td>
            </tr>
            <tr>
                <th>date</th><td id="date"></td>
            </tr>
            <tr>
                <th>day</th><td id="day"></td>
            </tr>
            <tr>
                <th>day-Kor</th><td id="dayKor"></td>
            </tr>
            <tr>
                <th>3/20/2014</th><td id="inputDate"></td>
            </tr>
            <tr>
                <th>ReverseToStr</th><td id="reverseToStr"></td>
            </tr>
    </tbody></table>
</div>

-- Script

$(document).ready(function(){
    var today = new Date();
    //alert(today);
    $('#today').text(today);
    $('#year').text(today.getYear());
    $('#fullYear').text(today.getFullYear());
    $('#month').text(today.getMonth());
    $('#month_add').text(today.getMonth()+1);
    $('#date').text(today.getDate());
    $('#day').text(today.getDay());
    $('#dayKor').text(dayToKor(today.getDay()));
 
    $('#inputDate').text(new Date('3/20/2014 17:30:30'));
    $('#reverseToStr').text('2014-03-20'.replace(/-/gi,''));
});

function dayToKor(num){
    var week = ["일","월","화","수","목","금","토"];  
    return week[num];
}

-- Result

todayThu Mar 20 2014 11:53:54 GMT+0900 (대한민국 표준시)
year114
fullYear2014
month2
month_add3
date20
day4
day-Kor
3/20/2014Thu Mar 20 2014 17:30:30 GMT+0900 (대한민국 표준시)
ReverseToStr20140320

댓글 없음:

댓글 쓰기