【问题标题】:Adding days to Selected Date with jquery or Javascript使用 jquery 或 Javascript 将日期添加到选定日期
【发布时间】:2015-12-14 13:14:27
【问题描述】:

我有两个“文本框”,我正在使用 jquery Datepicker 在“文本框1”中选择日期。当我在 Textbox1 中选择日期时,我需要在 Textbox2 中自动显示一个日期,并在 Textbox1 中选择两天。

例如:如果在“Textbox1”中选择的日期是“31/12/2015”

在“Textbox2”中显示日期为 02/02/2016...

我还需要添加月份和年份才能添加并显示在 Textbox2 中。

任何建议............?

【问题讨论】:

  • 你为此做了什么?
  • 欢迎来到 StackOverflow。请展示您到目前为止所做的工作,然后提出一个更具体的问题,这将更有可能获得您正在寻找的答案。

标签: javascript jquery date


【解决方案1】:
$("#textbox1").datepicker({
    onSelect: function(dateText, inst) {
       var someDate=new Date(dateText);
       var duration =2; //day
       $("#textbox2").val(new Date(someDate.getTime() +  (duration * 24 * 60 * 60 * 1000)));
    }
});​

【讨论】:

    【解决方案2】:

    我可能会使用momentjs 来添加日期,它比尝试在纯 JS 中手动添加日期要干净得多。请参阅下面的代码库以执行您在问题中描述的操作。

    $(function() {
    debugger;
      $('#textbox1').datepicker(
      {
        onSelect: function(dateText, inst) {
          var txtBox2 = $('#textbox2');
          var _sel = new moment(dateText);
          txtBox2.val(_sel.add('day',2));
        }
      });
    });
    <link href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
    <div>
      <input type="text" name="textbox1" id="textbox1">
      <input type="text" name="textbox2" id="textbox2" readonly="readonly">
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多