代码之家  ›  专栏  ›  技术社区  ›  suresh

在日期字段中显示默认的明天日期和时间为8Am

  •  0
  • suresh  · 技术社区  · 6 年前

    嗨,我想在我的日期栏显示明天的日期和时间(早上8点)。因为我试过一些代码 它只显示明天的日期而不显示时间。谁能帮我默认显示上午8点的时间

         <div class="col-xs-6 date" style="display:none;">
                                <input class="form-control" type="date" id="inputDate"/>
                            </div>
    
        <script>
        // Declare variables
        var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
        // Set values
        $("#inputDate").val(getFormattedDate(tomorrow));
        // Get date formatted as YYYY-MM-DD
        function getFormattedDate (date) {
            return date.getFullYear()
                + "-"
                + ("0" + (date.getMonth() + 1)).slice(-2)
                + "-"
                + ("0" + date.getDate()).slice(-2);
        }
    </script>
    

    有人能帮我怎么显示时间默认为上午8点和更晚如果用户想改变他们可以改变他们的日期和时间根据他们的要求。

    提前谢谢

    3 回复  |  直到 6 年前
        1
  •  2
  •   Omi    6 年前

    datetime-local . 以便支持date&时间。 然后使用 moment 设置日期格式。

    $("#inputDate").val(moment().add(1, 'days').hours(8).startOf('hour').format("YYYY-MM-DDTHH:mm:ss"));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
    <div class="col-xs-6 date" style="display:block;">
      <input class="form-control" type="datetime-local" id="inputDate" value="" />
    </div>
        2
  •  2
  •   Pritam Jana    6 年前

    http://momentjs.com/downloads/moment.js

    var tomorrow = moment(new Date())
                  .add(1,'days')
                  .hours(8)
                  .minutes(30)
                  .format('DD/MMM/YYYY HH:mm');
    
    console.log(tomorrow)
    
        3
  •  1
  •   Arun Kumar    6 年前

    var a = new Date; 
    a.setDate(a.getDate()+1); 
    a.setHours(8); 
    a.setMinutes(0); 
    a.setSeconds(0); 
    console.log(a);