0

我有 3 个按钮,即一周,上一个和下一个。单击星期按钮时,它将显示在文本框中选择的日期并添加 6 天(for example 26 Jun 2013-2 Jul 2013 )。单击上一个按钮时,它将19 Jun 2013-26 Jun 2013在文本框中显示。单击下一个按钮时,它将显示2 Jul 2013-8 Jul 2013在文本框中。这个过程还在继续。我想知道这将如何完成javascript

enter code here


 <input type="text" id="txtdatetimeshow" readonly="readonly"  
   style="height: 25px; width: 150px;
   font-size: medium; font-family: Times New Roman;"   runat="server"    />

    <input type="button" id="showweekbtn" runat="server" value="week"/>
    <input type="button" id="sfprevbtn" runat="server" value="prev"/>
    <input type="button" id="sfnextbtn" runat="server" value="next"/>

<script type="text/javascript">
$(document).ready(function()
    {

    var today=new Date();
    var date=today.getDate();
    var month=today.getMonth();
    var year= today.getFullYear();
    var month_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",   
    "Aug", "Sep", "Oct", "Nov", "Dec");

    var NextDate= new Date(year, month, date);//today's date
    var weekdate=new Date(NextDate);
    weekdate.setDate(weekdate.getDate()+6);//add 6 days to the date
    var Ndate=NextDate.getDate()+" "+month_names[month]+"  "+NextDate.getFullYear()+"-"+" "+weekdate.getDate()+"   "+month_names[weekdate.getMonth()]+" "+weekdate.getFullYear();

    var nextWeekDate=new Date(weekdate);
    nextWeekDate.setDate(weekdate.getDate()+6);
    var NWdate=weekdate.getDate()+" "+month_names[weekdate.getMonth()]+" "+weekdate.getFullYear()+"-"+""+nextWeekDate.getDate()+"  "+month_names[nextWeekDate.getMonth()]+" "+nextWeekDate.getFullYear();

    var prevWeekDate=new Date(NextDate);
    prevWeekDate.setDate(NextDate.getDate()-6);//subtract 6 days to date
    var PWdate=prevWeekDate.getDate()+" "+month_names[prevWeekDate.getMonth()]+" "+prevWeekDate.getFullYear()+"-"+NextDate.getDate()+"  "+month_names[NextDate.getMonth()]+" "+NextDate.getFullYear();

    //to show previous date
    $("#sfprevbtn").click(function() 
        {
       var prevDate=new Date(year,month,date-1);
       var pdate=prevDate.getDate()+" "+month_names[month] +" "+prevDate.getFullYear();
       $("#txtdatetimeshow").val(PWdate);

    });
    //to show next date
    $("#sfnextbtn").click(function()
         {

        var nextDate=new Date(year,month,date+1);
        var Ndate=nextDate.getDate()+" "+month_names[month] +" "+nextDate.getFullYear();
        $("#txtdatetimeshow").val(NWdate);
    });

        //to show week view
        $("#showweekbtn").click(function() 
        {
          $("#txtdatetimeshow").val(Ndate);

         });


          });

</script>
4

4 回答 4

0

我认为您想要的是一些 JavaScript,当您单击下一个/上一个按钮时,它将继续增加/减少周范围,而不仅仅是当前一周之前/之后的一周。

您的原始代码的问题在于您实际上硬编码了下一周/上一周的字符串,因为您的下一个/上一个按钮实际上并没有改变它们的值。我根据您的代码编写了一些代码(我希望)您正在寻找:

$(document).ready(function()
{
    var current=new Date();
    var month_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",   
    "Aug", "Sep", "Oct", "Nov", "Dec");
    getWeek(0);

    //to show previous date
    $("#sfprevbtn").click(function() 
    {
       getWeek(-1);
    });

    //to show next date
    $("#sfnextbtn").click(function()
    {
        getWeek(1);
    });

    //to show week view
    $("#showweekbtn").click(function() 
    {
        current=new Date();
        getWeek(0);
    });

    function getWeek(weeks){
        current = new Date(current.getTime() + weeks * 6 * 24 * 60 * 60 * 1000)
        var newDate = new Date(current.getTime() + 6 * 24 * 60 * 60 * 1000);
        var dateString=current.getDate()+" "+month_names[current.getMonth()] +" "+current.getFullYear() + " - " + newDate.getDate()+" "+month_names[newDate.getMonth()] +" "+newDate.getFullYear();
        $("#txtdatetimeshow").val(dateString);
    }
});

你会注意到我写了一个函数,getWeek(),它根据初始日期计算下一周的间隔。“当前”日期从今天的日期开始,但随着您单击下一个/上一个以查看不同的周间隔,相应地增加/减少。“星期”按钮此时并不是真正需要的,但我把它留在那里,所以如果你点击它,它会让你回到当前日期的星期。

我希望这能够帮到你。如果这不是您要找的东西,请告诉我,我很乐意为您提供进一步的帮助。快乐的脚本!

于 2013-06-26T16:59:48.790 回答
0

考虑到您对另一个答案的评论:

JavaScript

var date = new Date();

/* IMPORTANT PART */
date.setDate(date.getDate() + 6);
/* ------ */
var d = (date.getDate().toString()+" "+(parseInt(date.getMonth(),10) +1)+ " "+date.getFullYear().toString()).split(" ");

if(d[0].length == 1)
    d[0] = "0"+d[0];

if(d[1].length == 1)
    d[1] = "0"+d[1];

alert(d.join(" "));
于 2013-06-26T17:07:48.870 回答
0

尝试:

var date = new Date('2013','06','26');
date.setDate(date.getDate() + 6);
于 2013-06-26T07:01:23.003 回答
0

尝试这个:

函数显示日期(){

var 今天=新日期();

        var date=today.toISOString().slice(0, -14);
        // Strip last 14 characters, ISO format is like
        // 2012-12-30T17:41:49.027Z but we want
        // 2012-12-30

        document.getElementById("fname").value=date;
     }
于 2014-01-31T09:30:49.963 回答