在文本框中显示日期

时间:2013-06-26 06:38:09

标签: javascript html label

我有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 个答案:

答案 0 :(得分:0)

尝试:

var date = new Date('2013','06','26');
date.setDate(date.getDate() + 6);

答案 1 :(得分: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(),它根据初始日期计算下一周的间隔。 “当前”日期从今天的日期开始,但是当您单击next / prev以查看不同的周间隔时,相应地增加/减少。此时“星期”按钮并不是必需的,但是我把它留在那里,所以如果点击它,它将返回到当前日期的那一周。

我希望这会帮助你。如果这不是您想要的,请告诉我,我将很高兴为您提供进一步的帮助。快乐的脚本!

答案 2 :(得分: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(" "));

答案 3 :(得分:0)

试试这个:

  

function displayDate()   {

var today = new Date();

        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;
     }