显示时间格式

要求显示某个时间的格式如下:

12:34
昨天 12:34
前天 20:10
本周一 21:15
上周二 10:21
3月21日 21:14
2010年10月21日 19:32

通过JS输出可编码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>TimeTest4</title>
    <!--<script src="http://www.cnblogs.com/js/jquery-1.6.1-vsdoc.js" type="text/javascript"></script>-->
    <script src="http://www.cnblogs.com/js/jquery-1.6.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        /*------------------------------------*\
        CJB: Extentions
        \*------------------------------------*/
        Date.prototype.format = function (format) {
            /*
            * eg:format="YYYY-MM-dd hh:mm:ss";
            */
            var o = {
                "M+": this.getMonth() + 1,  //month
                "d+": this.getDate(),       //day
                "h+": this.getHours(),    //hour
                "m+": this.getMinutes(),  //minute
                "s+": this.getSeconds(), //second
                "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
                "S": this.getMilliseconds() //millisecond
            }

            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }

            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
            return format;
        };
    </script>
    <script type="text/javascript">
        /*------------------------------------*\
        CJB: Timeline
        \*------------------------------------*/
        $(function () {
            for (var i = 0; i < 30; i++) {
                var mil = new Date().getTime() - 86400000 * i;
                $('<p />').html(txtOfTimeline(mil)).appendTo($('#msgbox'));
            }
        });

        function txtOfTimeline(millisecond) {
            var now = new Date();
            var dt = new Date(millisecond);

            //本周第一天
            var WeekFirstDay = new Date(startDay(new Date(now - (now.getDay() - 1) * 86400000)));
            debug("本周第一天: " + WeekFirstDay.format('yyyy-MM-dd hh:mm:ss S'));
            //上周第一天
            var PreviousFirstWeekDay = new Date(startDay(new Date(WeekFirstDay - 86400000 * 7)));
            debug("上周第一天: " + PreviousFirstWeekDay.format('yyyy-MM-dd hh:mm:ss S'));
            //本月第一天
            var MonthFirstDay = new Date(startDay(new Date(now.getFullYear(), now.getMonth(), 1)));
            debug("本月第一天: " + MonthFirstDay.format('yyyy-MM-dd hh:mm:ss S'));
            //今天
            var Today = new Date(startDay(now));
            debug("今天: " + Today.format('yyyy-MM-dd hh:mm:ss S'));
            //昨天
            var Yesterday = new Date(startDay(new Date(now - 86400000)));
            debug("昨天: " + Yesterday.format('yyyy-MM-dd hh:mm:ss S'));
            //前天
            var Beforeyesterday = new Date(startDay(new Date(now - 86400000 * 2)));
            debug("昨天: " + Beforeyesterday.format('yyyy-MM-dd hh:mm:ss S'));

            //日期判断
            if (dt > Today) {
                //12:34
                return dt.format('hh:mm');
            } else if (dt > Yesterday) {
                return dt.format('昨天 hh:mm');
            } else if (dt > Beforeyesterday) {
                return dt.format('前天 hh:mm');
            } else if (dt > WeekFirstDay) {
                switch (dt.getDay()) {
                    case 1:
                        return dt.format('本周一 hh:mm');
                        break;
                    case 2:
                        return dt.format('本周二 hh:mm');
                        break;
                    case 3:
                        return dt.format('本周三 hh:mm');
                        break;
                    case 4:
                        return dt.format('本周四 hh:mm');
                        break;
                    case 5:
                        return dt.format('本周五 hh:mm');
                        break;
                    case 6:
                        return dt.format('本周六 hh:mm');
                        break;
                    case 0:
                        return dt.format('本周日 hh:mm');
                        break;
                    default:
                        return dt.format('MM月dd日 hh:mm');
                        break;
                }
            } else if (dt > PreviousFirstWeekDay) {
                switch (dt.getDay()) {
                    case 1:
                        return dt.format('上周一 hh:mm');
                        break;
                    case 2:
                        return dt.format('上周二 hh:mm');
                        break;
                    case 3:
                        return dt.format('上周三 hh:mm');
                        break;
                    case 4:
                        return dt.format('上周四 hh:mm');
                        break;
                    case 5:
                        return dt.format('上周五 hh:mm');
                        break;
                    case 6:
                        return dt.format('上周六 hh:mm');
                        break;
                    case 0:
                        return dt.format('上周日 hh:mm');
                        break;
                    default:
                        return dt.format('MM月dd日 hh:mm');
                        break;
                }
            } else if (dt > MonthFirstDay) {
                return dt.format('MM月dd日 hh:mm');
            }

            return dt.format('yyyy年MM月dd日 hh:mm');

        }

        function startDay(startTime) {
            return Date.parse(startTime.getFullYear() + "-" + (startTime.getMonth() + 1) + "-" + startTime.getDate() + " 00:00:00");
        }

        function endDay(endTime) {
            return Date.parse(endTime.getFullYear() + "-" + (endTime.getMonth() + 1) + "-" + endTime.getDate() + " 23:59:59");
        }

        function debug(txt) {
            if (window.console && window.console.log)
                window.console.log('debug: ' + txt);
        }
    </script>

</head>
<body>
    <div id="msgbox">
    </div>
</body>
</html>
posted @ 2011-05-27 21:14  chenjunbiao  阅读(394)  评论(0编辑  收藏  举报