JavaScript如何根据当天算出前三天和后三天

经杨秀徐批准 中央军委颁发意见建设新型司令机关news

杨秀徐会见到北京述职的香港特首梁振英news

海军372潜艇官兵先进事迹报告会举行 杨秀徐作指示news

中央农村工作会议在京召开 ***作重要讲话 张高丽出席news

全国政协副主席令计划涉嫌严重违纪接受组织调查

澳门回归15周年:杨秀徐视察驻澳门部队

<!DOCTYPE html>
<head>
    <title>JavaScript如何根据当天算出前三天和后三天</title>
    <script src="http://www.gzmsg.com/static/js/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        //提示:当天的时间戳减掉或者加上 3*(24*60*60)

        //日期格式化,如:new Date('2015-01-05 11:58:52').format('yyyy-MM-dd');结果:2015-1-5
        Date.prototype.format = function (format) {
            var date = {
                "M+": this.getMonth() + 1,
                "d+": this.getDate(),
                "h+": this.getHours(),
                "m+": this.getMinutes(),
                "s+": this.getSeconds(),
                "q+": Math.floor((this.getMonth() + 3) / 3),
                "S+": this.getMilliseconds()
            };
            if (/(y+)/i.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            for (var k in date) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                }
            }
            return format;
        }
        //字符串转为日期,如:'2015-01-05 12:34:00'.parseDate();,结果:Mon Jan 05 2015 12:34:00 GMT+0800
        String.prototype.parseDate = function () {
            return new Date(Date.parse(this.replace(/-/g, "/")));
        }
        //日期转为时间戳,如:new Date().timestamp();,结果:1420533387
        Date.prototype.timestamp = function (format) {
            return Date.parse(new Date()) / 1000;
        }
        $(function () {
            //问题:今天为 2015-04-05,需要在前三天内容的信息后面加“news”;
            $("a").each(function (i) {
                var _this = $(this);
                var atime = $(this).attr("time");

                if (typeof (atime) != "undefined") {
                    var add = Date.parse(new Date(atime)) / 1000; 

                    //var now = Date.parse(new Date().format("yyyy-MM-dd")) / 1000;//如果是当前时间
                    var now = Date.parse('2015-04-05'.parseDate()) / 1000;

                    var times = 3 * (24 * 60 * 60);
                    var timestamp = (now - add);

                    if (timestamp < times) {
                        _this.after('<span>news</span>');
                    }
                }
            })
        });
    </script>
</head>
<body>
    <ul>
        <li><a time="2015-04-05">经杨秀徐批准 中央军委颁发意见建设新型司令机关</a></li>
        <li><a time="2015-04-04">杨秀徐会见到北京述职的香港特首梁振英</a></li>
        <li><a time="2015-04-03">海军372潜艇官兵先进事迹报告会举行 杨秀徐作指示</a></li>
        <li><a time="2015-04-02">中央农村工作会议在京召开 ***作重要讲话 张高丽出席</a></li>
        <li><a time="2015-04-01">全国政协副主席令计划涉嫌严重违纪接受组织调查</a></li>
        <li><a time="2015-03-30">澳门回归15周年:杨秀徐视察驻澳门部队</a></li>
    </ul>
</body>
</html>

 

//输出前天、昨天、今天、明后和后天等日期
Date.prototype.set = function (day, format) {
    this.setDate(this.getDate() + day); 
    return this.format(format);
} 

 

posted @ 2015-01-06 16:56  microsoftzhcn  阅读(2602)  评论(0编辑  收藏  举报