JQ获取今天和昨天还有一周的时间代码
html页面代码:
<div class="date-list-center">
<a href="javascript:;" value="0">今天</a>
<a href="javascript:;" value="1">昨天</a>
<a href="javascript:;" value="2">一周</a>
<a href="javascript:;" value="3">全部</a>
</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();}
})