用js计算出今天是一年中的第几天

<script type="text/javascript">
        var dateArr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        var date = new Date();
        var day = date.getDate();
        var month = date.getMonth(); //getMonth()是从0开始
        var year = date.getFullYear();
        var result = 0;
        for (var i = 0; i < month; i++) {
            result += dateArr[i];
        }
        result += day;
        //判断是否闰年
        if (month > 1 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            result += 1;
        }
        alert("今天是" + year + "年的第" + result + "天");
    </script>

 

posted @ 2014-12-24 15:15  lanyan  阅读(2598)  评论(0编辑  收藏  举报