当前位置: 首页 >> 新闻中心
新闻中心
JS获取任意日期(昨天,今天,明天,上周,本月,本年,季度...)

<table>
<tr>
<td class="td_s" bgcolor="#c0f0fb">
<select name="stdDateFilter" onchange='showDate(this.value )'>
<option value="custom">自定义</option>
<option value="prevfy">上年</option>
<option value="thisfy">本年</option>
<option value="nextfy">下年</option>
<option value="prevfq">上季度</option>
<option value="thisfq">本季度</option>
<option value="nextfq">下季度</option>
<option value="yesterday">昨天</option>
<option value="today">今天</option>
<option value="tomorrow">明天</option>
<option value="lastweek">上星期</option>
<option value="thisweek">本星期</option>
<option value="nextweek">下星期</option>
<option value="lastmonth">上月</option>
<option selected value="thismonth">本月</option>
<option value="nextmonth">下月</option>>
</select>
</td>
<td  class="td_s"colspan="12" bgcolor="#c0f0fb">&nbsp;&nbsp;
开始日期: <input type="text" id="startTime" name="startTime" />
结束日期: <input type="text" id="endTime" name="endTime" />
&nbsp;&nbsp;<input type="submit" value="查询"/></td>
</tr>
</table>
<script type="text/javascript">

function getMonday(){
 var d = new Date();
 var year = d.getFullYear();
 var month = d.getMonth()+1;
 var date = d.getDate();

 // 周
 var day=d.getDay();
 var monday = day!=0?day-1:6; // 本周一与当前日期相差的天数

 return monday;
}

function getMonth(type,months){
 var d = new Date();
 var year = d.getFullYear();
 var month = d.getMonth()+1;

 if(months!=0){
  // 如果本月为12月,年份加1,月份为1,否则月份加1。
  if(month==12 && months>0){
   year++;month=1;
  }else if(month==1 && months<0){
   year--;month=12;
  }else{
   month = month+months;
  }
 }
    var date = d.getDate();
    var firstday=year+"-"+month+"-"+1;
 var lastday="";
 if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
  lastday = year+"-"+month+"-"+31;
 }else if(month==2){
 // 判断是否为闰年(能被4整除且不能被100整除 或 能被100整除且能被400整除)
 if((year%4==0 && year%100!=0)||(year%100==0 && year%400==0)){
  lastday = year+"-"+month+"-"+29;
 }else{
  lastday = year+"-"+month+"-"+28;
 }
 }else{
  lastday = year+"-"+month+"-"+30;
 }
 var day = "";
 if(type=="s"){
  day = firstday;
 }else{
  day = lastday;
 }
 return day;
}

function getQFMonth(month){
var quarterMonthStart=0;
var spring=1; //春 
var summer=4; //夏 
var fall=7;   //秋 
var winter=10;//冬 
if(month<3){ return spring; }
if(month<6){ return summer; }
if(month<9){ return fall; }
return winter;
};

function getQF(type,months){
var d = new Date();
var year = d.getFullYear();
    var month = d.getMonth()+1;
    var qfmonth = getQFMonth(month);

    if(months!=0){
    if(qfmonth==10 && months>0){
    year++;qfmonth=1;
    }else if(qfmonth==1 && months<0){
    year--;qfmonth=10;
    }else{
    qfmonth = qfmonth+months;
    }
    }
   
    var fd = year+"-"+qfmonth+"-"+1;
    var ed = "";
    if(qfmonth==1 || qfmonth==10){
ed = year+"-"+(qfmonth+2)+"-"+31;
    }else{
ed = year+"-"+(qfmonth+2)+"-"+30;
    }

    var qf = "";
if(type=="s"){
qf = fd;
}else{
qf = ed;
}
return qf;
}

function getYears(type,years){
 var d = new Date();
 var year = d.getFullYear();

    var fd = (year+years)+"-"+01+"-"+01;
    var ed = (year+years)+"-"+12+"-"+31;

    var yr = "";
 if(type=="s"){
  yr = fd;
 }else{
  yr = ed;
 }
 return yr;
}

function showDate(type)
{
if (type!="custom")
{
 document.getElementById("startTime").readOnly=true;
 document.getElementById("endTime").readOnly=true;
}
else
{
 document.getElementById("startTime").readOnly=false;
 document.getElementById("endTime").readOnly=false;
}
if( type == "today" )// 当前日期
{
 document.getElementById("startTime").value = getDate(0);
 document.getElementById("endTime").value = getDate(0);
}
else if( type == "yesterday" )// 昨天
{
document.getElementById("startTime").value = getDate(-1);
document.getElementById("endTime").value = getDate(-1);
}
else if( type == "tomorrow" ) // 明天
{
document.getElementById("startTime").value = getDate(1);
document.getElementById("endTime").value = getDate(1);
}
else if( type == "thisweek" )// 本周
{
document.getElementById("startTime").value = getDate(-getMonday());
document.getElementById("endTime").value = getDate(-getMonday()+6);
}
else if( type == "lastweek" )// 上周
{
document.getElementById("startTime").value = getDate(-getMonday()-7);
document.getElementById("endTime").value = getDate(-getMonday()-1);
}
else if( type == "nextweek" )// 下周
{
document.getElementById("startTime").value = getDate(-getMonday()+7);
document.getElementById("endTime").value = getDate(-getMonday()+13);
}
else if( type == "thismonth" )// 本月
{
document.getElementById("startTime").value = getMonth("s",0);
document.getElementById("endTime").value = getMonth("e",0);
}
else if( type == "lastmonth" )// 上个月
{
document.getElementById("startTime").value = getMonth("s",-1);
document.getElementById("endTime").value = getMonth("e",-1);
}
else if( type == "nextmonth" )// 下个月
{
document.getElementById("startTime").value = getMonth("s",1);
document.getElementById("endTime").value = getMonth("e",1);
}
else if( type == "thisfy" )// 本年
{
document.getElementById("startTime").value = getYears("s",0);
document.getElementById("endTime").value = getYears("e",0);
}
else if( type == "prevfy" )// 去年
{
document.getElementById("startTime").value = getYears("s",-1);
document.getElementById("endTime").value = getYears("e",-1);
}
else if( type == "nextfy" )// 明年
{
document.getElementById("startTime").value = getYears("s",1);
document.getElementById("endTime").value = getYears("e",1);
}
else if( type == "nextfq" )// 下季度
{
document.getElementById("startTime").value = getQF("s",3);
document.getElementById("endTime").value = getQF("E",3);
}
else if( type == "prevfq" )// 上季度
{
document.getElementById("startTime").value = getQF("s",-3);
document.getElementById("endTime").value = getQF("E",-3);
}
else if( type == "thisfq" )// 本季度
{
document.getElementById("startTime").value = getQF("s",0);
document.getElementById("endTime").value = getQF("e",0);
}
else
{
document.getElementById("startTime").value = "";
document.getElementById("endTime").value = "";
}
}

function getDate(dates){
 var dd = new Date();
 dd.setDate(dd.getDate()+dates);
 var y = dd.getFullYear();
 var m = dd.getMonth()+1;
 var d = dd.getDate();
 return y+"-"+m+"-"+d;
}

</script>