jq获取今天、昨天、一周时间

不少后台文章管理系统就有今天、明天、一周内、全部的分类展示,用Jquery获取今天、明天、一周内的时间节点(如下图)

html页面代码:

1 <div class="date-list-center">
2     <a href="javascript:;" value="0">今天</a>
3     <a href="javascript:;" value="1">昨天</a>
4     <a href="javascript:;" value="2">一周</a>
5     <a href="javascript:;" value="3">全部</a>
6 </div>

jquery部分:

今天:直接new Date();

昨天:今天减去一天(oneday = 1000*60*60*24);

一周:今天减去一天*7;

最后用format格式化

$('.date-list-center').delegate('a','click',function(search){
    var Text = $(this).text();
    var today = new Date();
    var oneday = 1000 * 60 * 60 * 24;
    var format='yyyy-MM-dd hh:mm:ss';
    var begindate;
    switch(Text)
    {
    //今天
        case '今天':
        begindate=new Date();
        break;
    //昨天
    case '昨天':
        begindate=new Date(today - oneday);
        break;
    //一周
        case '一周':
        begindate=new Date(today- oneday * 7);
        break;
    if(begindate){
        begindate.setHours(0);
        begindate.setMinutes(0);
        begindate.setSeconds(0);
        begindate.setMilliseconds(0);
    }
    if(today){
        today.setHours(23);
        today.setMinutes(59);
        today.setSeconds(59);
        today.setMilliseconds(59);
    }
//format时间格式化
    search.begin = begindate==null?null:begindate.format(format);
    search.end = today==null?null:today.format(format);
    alert(search.begin+' '+search.end);
    if(callback){callback();}
})    
原文地址:https://www.cnblogs.com/realdanielwu/p/8066293.html