添加输入框值到日期

时间:2018-02-05 09:54:15

标签: javascript jquery html datepicker

我正在尝试从输入框添加日期和整数值以形成新日期。我已应用以下代码,但这显示新的日期形成为NANANA。我附上了我的代码。我能否就此提出意见和建议。

$(document).ready(function () {
   $('#txtDate').datepicker();
});

function getdate() {
var tt = document.getElementById('txtDate').value;
var input = document.getElementById('extdays').value;
var date = new Date(tt);
var newdate = new Date(date);


newdate.setDate(newdate.getDate() + input);

var dd = newdate.getDate();
var mm = newdate.getMonth() + 1;
var y = newdate.getFullYear();

var someFormattedDate = dd + '/' + mm + '/' + y;
document.getElementById('follow_Date').value = someFormattedDate;
}    // console.log(someFormattedDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
    <div class="col-md-6">
        <div class="col-md-4">
            <label for="username" class="control-label"><b>Date from:</b></label>
        </div>
        <div class="col-md-8">
            <input id="txtDate" type="text" name="title" maxlength="150" class="form-control" placeholder="Start Date" value="">
        </div>
    </div>
    <div class="col-md-6">
        <div class="col-md-4">
            <label for="username" class="control-label"><b>New Date formed:</b></label>
        </div>
        <div class="col-md-8">
            <div class="input-group date">
                <input id="follow_Date" type="text" name="days_for_start" disabled class="form-control" value="">
            </div>
        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
    <input type="button" onclick="getdate()" value="Check New Date" />
    <div class="col-md-6">
        <div class="col-md-4">
            <label for="username" class="control-label"><b>Extension days:</b></label>
        </div>
        <div class="col-md-8">
            <input id="extdays" type="text" name="days_for_end" class="form-control" required="" value="">
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</div>

enter image description here

2 个答案:

答案 0 :(得分:1)

最初你没有任何输入值,所以检查输入为null然后将其初始化为0.还使用parseInt进行输入。请检查: - enter image description here

$(document).ready(function () {
           $('#txtDate').datepicker();
  });
   $( "#txtDate" ).datepicker({
        dateFormat: "dd/mm/yy",
        minDate:new Date()
    });

function getdate() {
    var tt = document.getElementById('txtDate').value;
    var input = document.getElementById('extdays').value;
    if (input == "" || input==null)
    {
      input =0
    }
   

var initial = tt.split(/\//).reverse().join('/'); // to change the format

    var date = new Date(initial);
    var newdate = new Date(date);


   newdate.setDate(newdate.getDate() + parseInt(input));

    var dd = newdate.getDate();
    var mm = newdate.getMonth() + 1;
    var y = newdate.getFullYear();

    var someFormattedDate = dd + '/' + mm + '/' + y;
    document.getElementById('follow_Date').value = someFormattedDate;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="form-group">
    <div class="col-md-6">
        <div class="col-md-4">
            <label for="username" class="control-label"><b>Date from:</b></label>
        </div>
        <div class="col-md-8 input-group date" data-provide="datepicker">
            <input id="txtDate" type="text" name="title" maxlength="150"class="form-control" placeholder="Start Date" value="">
        </div>
    </div>
    <div class="col-md-6">
        <div class="col-md-4">
            <label for="username" class="control-label"><b>New Date formed:</b></label>
        </div>
        <div class="col-md-8">
            <div class="input-group date">
                <input id="follow_Date" type="text" name="days_for_start" disabled class="form-control" value="">
            </div>
        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
    <input type="button" onclick="getdate()" value="Check New Date" />
    <div class="col-md-6">
        <div class="col-md-4">
            <label for="username" class="control-label"><b>Extension days:</b></label>
        </div>
        <div class="col-md-8">
            <input id="extdays" type="text" name="days_for_end" class="form-control" required="" value="">
        </div>
    </div>
</div>

答案 1 :(得分:0)

我建议你在datepicker上使用onSelect事件代替你onclick回调:

 UPDATE a
 SET is_active = '0'  
 FROM Restaurants a
 INNER JOIN Users  b
 ON a.id = b.id  
 WHERE b.email = 'something@gmail.com'; 

看看那里:http://jsfiddle.net/x6jfazch/