如何在文本框中获取上一个和下一个日期

时间:2013-06-25 08:30:36

标签: javascript html

我有一个文本框和两个按钮。

  • button1名称为prev
  • button2名称为next
  • textbox包含current date
If I click in next button the textbox will show the next date.
If I click in prev button the textbox will show the previous date.

请帮帮我......

enter code here
<input type="text" id="txtDateFilter" readonly="readonly" runat="server" />

<div id="sfprevbtn" title="Prev" class="fbutton">
    <span class="fprev"></span>
</div>
<div id="sfnextbtn" title="Next" class="fbutton">
    <span class="fnext"></span>
</div>

<script type="text/javascript">
$('#sfprevbtn').click(function(e)
{
    $('#txtDateFilter').val();
});

$('#sfnextbtn').click(function(e)
{
    $('#txtDateFilter').val();
});
</script>

1 个答案:

答案 0 :(得分:3)

看看我制作的这个小提琴:http://jsfiddle.net/TV57Q/

我使用了javascript本身的Date对象。

var date = new Date();
$(function () {
    // This writes out the current date to the text field as starter
    $('#field').val(date.toLocaleDateString());

    // Hook up previous button event
    $('#prev').click(function () {
        // Set our current date to 1 day earlier
        date.setDate(date.getDate() - 1);
        // Write the result to our text input
        $('#field').val(date.toLocaleDateString())
    });
    // Hook up next button event
    $('#next').click(function () {
        // Set our current date to 1 day later
        date.setDate(date.getDate() + 1);
        // Write the result to our text input
        $('#field').val(date.toLocaleDateString())
    });
});