我是根本就不会js!但现在非得要js才行!

解决方案 »

  1.   

    顺便帖下我在winform时候实现这个需求的代码,供参考if(this.cmbTimeType.SelectedIndex==4) //指定日期查询
    {
    if(DateTime.Compare(this.dateTimePickerStart.Value,this.dateTimePickerEnd.Value.Date)>0)
    this.dateTimePickerStart.Value = this.dateTimePickerEnd.Value;
    }
    if(this.cmbTimeType.SelectedIndex==0)//日查询
    {
    if(DateTime.Compare(this.dateTimePickerStart.Value.Date,System.DateTime.Now.Date) >= 0)
    this.btnRight.Enabled = false;
    else this.btnRight.Enabled = true;
    this.dateTimePickerEnd.Value = this.dateTimePickerStart.Value;
    }
    if(this.cmbTimeType.SelectedIndex==1)//周查询
    {
    DateTime t = this.dateTimePickerStart.Value;
    while(t.DayOfWeek!=System.DayOfWeek.Sunday)
    t = t.AddDays(-1);
    this.dateTimePickerStart.Value = t;
    this.dateTimePickerEnd.Value  = t.AddDays(6);
    }
    if(this.cmbTimeType.SelectedIndex==2)//月查询
    {
    int year = int.Parse(this.dateTimePickerStart.Value.Year.ToString());
    int month = int.Parse(this.dateTimePickerStart.Value.Month.ToString());
    int curMonth = int.Parse(System.DateTime.Now.Month.ToString());
    int curYear = int.Parse(System.DateTime.Now.Year.ToString());
    int days = DateTime.DaysInMonth(year,month);
    this.dateTimePickerStart.Value = Convert.ToDateTime(year+"-"+month+"-01");
    if(month==curMonth&&year==curYear) this.dateTimePickerEnd.Value = System.DateTime.Now;
    else this.dateTimePickerEnd.Value =  Convert.ToDateTime(year+"-"+month+"-"+days);
    }
    if(this.cmbTimeType.SelectedIndex==3)//年查询
    {
    int year = int.Parse(this.dateTimePickerStart.Value.Year.ToString());
    int curYear = int.Parse(System.DateTime.Now.Year.ToString());
    this.dateTimePickerStart.Value = Convert.ToDateTime(year+"-01-01");
    if(year==curYear) this.dateTimePickerEnd.Value = System.DateTime.Now; 
    else this.dateTimePickerEnd.Value = Convert.ToDateTime(year+"-12-31");
    }
      

  2.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
    <HTML>  
    <HEAD>  
    <TITLE> New Document    </TITLE>  
    <script>
    function $(sId){
    return document.getElementById(sId);
    }
    window.onload = function (){
    var temp = new Date();
    var year = temp.getYear();
    var month = temp.getMonth()+1;
    var date  = temp.getDate();
    var now = year +"-" +month+"-"+date;
    $('txt2').value = $('txt1').value=now + " " + Date().split(" ")[3];}function changeNow(oSel){
    if(oSel.value == '每日统计'){
    $('txt2').value = $('txt1').value.split(" ")[0] + " " + "23:59:59";
    $('txt2').disabled = true;
    }
    if(oSel.value == '每周统计'){
    var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    var nowYear = new Date().getYear();
    var nowWeek = new Date().getDay();
    var nowMonth=new Date().getMonth()+1;
    var nowDate = "";
    var diffTime = nowWeek-6;
    var nowDate1 = MonHead[nowMonth];
    if((new Date().getMonth()+1)==2 && IsPinYear(new Date().getYear())){
    nowDate1++;
    }
    //alert(diffTime)
    if(diffTime<=0){
    nowDate = parseInt(new Date().getDate() )- parseInt(diffTime);
    if(nowDate>nowDate1){
    nowMonth++;
    nowDate = nowDate - nowDate1;
    if(nowMonth>12){
    nowYear++;
    nowMonth -=12;
    }
    }
    }
    $('txt2').value = nowYear+"-"+nowMonth+"-" + nowDate + " " + "23:59:59";
    $('txt2').disabled = true;
    } if(oSel.value == '每月统计'){
    var yA = new Date().getYear();
    var mB = new Date().getMonth()+1;
    $('txt2').value=yA+"-"+mB+"-"+getLastDay(yA,mB)+" "+"23:59:59";
    $('txt2').disabled = true;

    } if(oSel.value == '每年统计'){
    var n = '';
    var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    var oYear = new Date().getYear();
    n = MonHead [11];

    $('txt2').value =new Date().getYear() + "-" + "12" + "-" +n + " " + "23:59:59";
    $('txt2').disabled = true; } function getLastDay(year,month){
    var new_year = year;
    var new_month = month++;
    if(month>12){
    new_month -=12;
    new_year++;
    }
    var new_date = new Date(new_year,new_month,1);
    return (new Date(new_date.getTime()-1000*60*60*24 )).getDate();

    } function IsPinYear(year)//判断是否闰平年 
    {   return(0 == year%4 && (year%100 !=0 || year%400 == 0));} 
    }
    </script>
    </HEAD>  <BODY>  
    <select name='sel' onchange="changeNow(this)">
    <option value=''>--------</option>
    <option value='指定时间'>指定时间</option>
    <option value='每日统计'>每日统计</option>
    <option value='每周统计'>每周统计</option>
    <option value='每月统计'>每月统计</option>
    <option value='每年统计'>每年统计</option>
    </select><input type='text' value='' id='txt1'>
    <input type='text' value='' id='txt2'>
    </BODY>  
    </HTML>   
      

  3.   

    s_liangchao1s .....我得赞你一下....JS用的太好了
    但你没给人家日期控件.我这里也没有带时间功能的现成的控件.
      

  4.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
    <HTML>  
    <HEAD>  
    <TITLE> New Document    </TITLE>  
    <script>
    function $(sId){
    return document.getElementById(sId);
    }
    window.onload = function (){
    var temp = new Date();
    var year = temp.getYear();
    var month = temp.getMonth()+1;
    var date  = temp.getDate();
    var now = year +"-" +month+"-"+date;
    $('txt2').value = $('txt1').value=now + " " + Date().split(" ")[3];}function changeNow(oSel){
    if(oSel.value == '每日统计'){
    $('txt2').value = $('txt1').value.split(" ")[0] + " " + "23:59:59";
    $('txt2').disabled = true;
    }
    if(oSel.value == '每周统计'){
    var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    var nowYear = new Date().getYear();
    var nowWeek = new Date().getDay();
    var nowMonth=new Date().getMonth()+1;
    var nowDate = "";
    var upDate = "";
    var diffTime = nowWeek-6;
    var nowDate1 = MonHead[nowMonth-1];
    if((new Date().getMonth()+1)==2 && IsPinYear(new Date().getYear())){
    nowDate1++;
    }
    //alert(diffTime)
    if(diffTime<=0){
    nowDate = parseInt(new Date().getDate() )- parseInt(diffTime);
    if(nowDate>nowDate1){
    nowMonth++;
    nowDate = nowDate - nowDate1;
    if(nowMonth>12){
    nowYear++;
    nowMonth -=12;
    }
    }
    } upDate = nowDate1-6;
    if(upDate<1){
    nowMonth--;
    nowDate = MonHead[nowMonth-1];
    }
    $('txt1').value = nowYear+"-"+nowMonth+"-" + upDate + " " + "23:59:59";
    $('txt2').value = nowYear+"-"+nowMonth+"-" + nowDate + " " + "23:59:59";
    $('txt2').disabled = true;
    } if(oSel.value == '每月统计'){
    var yA = new Date().getYear();
    var mB = new Date().getMonth()+1;
    $('txt1').value=yA+"-"+mB+"-"+1+" "+"23:59:59"
    $('txt2').value=yA+"-"+mB+"-"+getLastDay(yA,mB)+" "+"23:59:59";
    $('txt2').disabled = true;

    } if(oSel.value == '每年统计'){
    var n = '';
    var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    var oYear = new Date().getYear();
    n = MonHead [11];
    $('txt1').value =new Date().getYear() + "-" + "1" + "-" +"1" + " " + "23:59:59";
    $('txt2').value =new Date().getYear() + "-" + "12" + "-" +n + " " + "23:59:59";
    $('txt2').disabled = true; } function getLastDay(year,month){
    var new_year = year;
    var new_month = month++;
    if(month>12){
    new_month -=12;
    new_year++;
    }
    var new_date = new Date(new_year,new_month,1);
    return (new Date(new_date.getTime()-1000*60*60*24 )).getDate();

    } function IsPinYear(year)//判断是否闰平年 
    {   return(0 == year%4 && (year%100 !=0 || year%400 == 0));} 
    }
    </script>
    </HEAD>  <BODY>  
    <select name='sel' onchange="changeNow(this)">
    <option value=''>--------</option>
    <option value='指定时间'>指定时间</option>
    <option value='每日统计'>每日统计</option>
    <option value='每周统计'>每周统计</option>
    <option value='每月统计'>每月统计</option>
    <option value='每年统计'>每年统计</option>
    </select><input type='text' value='' id='txt1'>
    <input type='text' value='' id='txt2'>
    </BODY>  
    </HTML>   
      

  5.   

    楼市代码有问题,6.29进行周统计,text2显示的是7-4号,不是周六。你们动作太快了,我去开个会你们就贴答案了,过分啊~~下面是我的,
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>统计</title>
    </head><body>
    <p>
      <select name="select1" onchange="cc(document.getElementById('select1').value);">
        <option value="0">请选择</option>
    <option value="1">指定时间</option>
        <option value="2">每日统计</option>
        <option value="3">每周统计</option>
        <option value="4">每月统计</option>
        <option value="5">每年统计</option>
      </select>
    </p>
    <p>
      <input name="time1" type="text" id="time1" />
      </p>
    <p>
      <input name="time2" type="text" id="time2" />
    </p>
    </body>
    </html>
    <script language="javascript">
    function cc(n)
    {
    var t1=document.getElementById("time1");
    var t2=document.getElementById("time2");
    //alert(n); 2008-5-29 23:59:59 
    var now= new Date(); 
    switch(n){   
        case   "1":
       t1.value=t2.value= now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
    t1.disabled=false;
    t2.disabled=false;
    break;   
        case   "2":       
       t1.value= now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
    t2.value=now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" 23:59:59";
    t1.disabled=false;
    t2.disabled=true;
    break;
      case   "3":       
       t1.value= now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
    //t2.value=now.getYear()+"-"+(now.getMonth()+1)+"-"+LastDayofWeek()+" 23:59:59";
    t2.value=LastDayofWeek();
    t1.disabled=false;
    t2.disabled=true;
    break;
    case   "4":         
    t1.value= now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
    t2.value=now.getYear()+"-"+(now.getMonth()+1)+"-"+LastDayofMonth()+" 23:59:59";
    t1.disabled=false;
    t2.disabled=true;
    break;
    case   "5":       
       t1.value= now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
    t2.value=now.getYear()+"-12-31 23:59:59";
    t1.disabled=false;
    t2.disabled=true;
    break;
        default:
      }
    }function LastDayofMonth()   
    {    
        var d  = new   Date();   
       d= new   Date(d.getFullYear(),d.getMonth()+1,0);   
        return d.getDate();
    }
    function LastDayofWeek()
    {
    var d  = new   Date();
    dd=d.getDate()+6-d.getDay();
    if (dd>LastDayofMonth())
    {
    var d1=new Date(d.getFullYear(),d.getMonth()+1,1); //下个月1号
    var d2=d1.getDate()+6-d1.getDay();
    return d.getFullYear()+"-"+(d.getMonth()+2)+"-"+d2+" 23:59:59";
    }
    else
    return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+dd+" 23:59:59";


    //return d.getYear()+"-"+d1.getMonth()+"-"+d1.getDate()+" 23:59:59";
    }
    </script>
      

  6.   

    很感谢各位,现在有些问题,我估计是在提问的时候忽略了
    问题1:第二个框的时间不能超过当前时间,就是说假如我现在选择的是按周统计,我现在第一个框的值是2008-5-26号,此时第一个框应该最终显示成5月25号,第二个框本来应该显示5月31号,但是今天才是5月29号,所以第二个框只能显示5月29号,月,年也是这样 
    问题2:第一个框的值可以变化,当我改变第一个框的值的时候哦,第二个框的值根据select 做相应的变动 
    问题3:比如现在我选择的是按周统计,我第一个框输入的值是2008-5-29号,这时候,第一个框需要变化成5月25号,第二个框需要变化成5月31号,也就是总是第一个框是输入的时间的当前星期的星期天,第二个框是输入时间的星期的星期六。月,和年也是这样
      

  7.   

    Look~<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript">function $(id){ return document.getElementById(id);}/*-----------------------日期管理类----------------------*/
    var DateManager = {
    /*******************************/
    /*CSDN.NavyMK
    /*费了些心思,留个名吧。
    /*******************************/
    addDay : function(daycount, oldtime){ //添加天数方法,也可在指定日期类上添加
    var dayM = 1000 * 60 * 60 * 24;
    var _orignalTime = new Date(0);
    if(oldtime) return new Date(oldtime - _orignalTime + dayM * daycount);
    else return new Date(new Date() - _orignalTime + dayM * daycount);
    }, setEndSeconds : function(now){
    now.setHours(23);
    now.setMinutes(59);
    now.setSeconds(59);
    return now;
    }, getLastMonthDay : function(now){
    if(!now) now = new Date();
    var month = now.getMonth() + 1;
    return new Date(now.getFullYear() + parseInt(month / 12), month % 12, 0, 23, 59, 59, 0);
    }, getLastWeekDay : function(now, offset){ //返回周末一天。js中周日为最后一天,offset为修正值。如要取周六可设置offset = -1.如果周跨月份,返回月末日。
    if(!now) now = new Date();
    if(!offset) offset = 0;
    var customDate = DateManager.addDay(7 - now.getDay() + offset, now);
    customDate = customDate.getMonth() != now.getMonth() ? DateManager.getLastMonthDay(now) : customDate;
    return DateManager.setEndSeconds(customDate);
    }, getLastYearDay : function(now){ //@#$%^...
    if(!now) now = new Date();
    return new Date(now.getFullYear(), 11, 31, 23, 59, 59, 0); 
    }, dateFormat : function(date){ //简单格式化时间,方便调试
    try{
    var dateSplit = arguments[1] ? arguments[1] : "-";
    var timeSplit = arguments[2] ? arguments[2] : ":";
    var Split = arguments[2] ? arguments[2] : " ";
    return [
    [date.getFullYear(), date.getMonth() + 1, date.getDate()].join(dateSplit), 
    [date.getHours(), date.getMinutes(), date.getSeconds()].join(timeSplit)
    ].join(Split);
    } catch(err) {
    return DateManager.dateFormat(new Date(0)); //如果date不是时间对象,返回初始值
    }
    }
    }
    /*-----------------------具体处理部分---------------------------*/
    //返回值
    function choose(type, now){
    if(!now) now = new Date();
    switch(type){
    case 1:
    return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.setEndSeconds(now))];
    case 2:
    return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.getLastWeekDay(now, -1))]; //周6
    case 3:
    return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.getLastMonthDay(now))]; //月末
    case 4:
    return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.getLastYearDay(now))]; //年末
    default: 
    return false;
    }
    }
    function setValue(idx, now){
    if(!now) now = new Date();
    var vals = choose(idx, now);
    if(vals){
    $("v1").value = vals[0];
    var v2 = $("v2");
    v2.value = vals[1];
    v2.disabled = idx > 1;
    } else {
    $("v1").value = "";
    $("v2").value = "";
    }
    }
    //如果客户端修改了v1的值,则根据值日期自动取得相关数据。该例没有日期格式化程序,注意日期和时间格式与预设统一
    function whenCustomChange(){
    var sel = $("sel");
    var date = $("v1").value.split(" ")[0].split("-");
    var time = $("v1").value.split(" ")[1].split(":");
    var customDate = new Date(date[0], parseInt(date[1]) - 1, date[2], time[0], time[1], time[2]);
    setValue($("sel").selectedIndex, customDate);
    }
    </script>
    </head>
    <body>
    <select name="sel" id="sel" onchange="setValue(this.selectedIndex)">
      <option>--指定时间--</option>
      <option>每日统计</option>
      <option>每周统计</option>
      <option>每月统计</option>
      <option>每年统计</option>
    </select>
    <input name="v1" type="text" id="v1" size="20" onchange="whenCustomChange()" /> 

    <input name="v2" type="text" id="v2" size="20" />
    </body>
    </html>DateManager这个类可以复用的.
      

  8.   

    哎,楼上的各位我都十分的感谢,但是现在我感觉情况是楼上各位写的明显跟我需求的相去甚远!所以我自己在努力学习JS,争取两天之内搞定!现在感觉JS也没我想的那么难!呵呵!再一次感谢各位!