简体   繁体   English

在引导程序日期时间选择器中禁用时间

[英]Disable time in bootstrap date time picker

I am using bootstrap date time picker in my web application, made in PHP/HTML5 and JavaScript.我在我的 web 应用程序中使用引导日期时间选择器,用 PHP/HTML5 和 JavaScript 制作。 I am currently using one from here: http://tarruda.github.io/bootstrap-datetimepicker/我目前正在使用这里的一个: http : //tarruda.github.io/bootstrap-datetimepicker/

When I am using the control without time, it doesn't work.当我没有时间使用控件时,它不起作用。 It just shows a blank text box.它只显示一个空白文本框。

I just want to remove time from date time picker.我只想从日期时间选择器中删除时间。 Is there any solution for this?有什么解决办法吗?

<div class="well">
    <div id="datetimepicker4" class="input-append"> 
        <input data-format="yyyy-MM-dd" type="text"></input> 
        <span class="add-on"> 
            <i data-time-icon="icon-time" data-date-icon="icon-calendar"> </i> 
        </span> 
    </div> 
</div> 
<script type="text/javascript"> 
    $(function() { 
        $('#datetimepicker4').datetimepicker({ pickTime: false }); 
    }); 
</script>

Check the below snippet检查下面的片段

<div class="container">
<div class="row">
    <div class='col-sm-6'>
        <div class="form-group">
            <div class='input-group date' id='datetimepicker4'>
                <input type='text' class="form-control" />
                <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span>
                </span>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(function() {              
           // Bootstrap DateTimePicker v4
           $('#datetimepicker4').datetimepicker({
                 format: 'DD/MM/YYYY'
           });
        });      
    </script>
</div>

You can refer http://eonasdan.github.io/bootstrap-datetimepicker/ for documentation and other functions in detail.您可以参考http://eonasdan.github.io/bootstrap-datetimepicker/ 详细了解文档和其他功能。 This should work.这应该有效。

Update to support i18n更新以支持 i18n

Use localized formats of moment.js:使用 moment.js 的本地化格式:

  • L for date only L仅用于日期
  • LT for time only LT仅限时间
  • L LT for date and time L LT表示日期和时间

See other localized formats in the moment.js documentation ( https://momentjs.com/docs/#/displaying/format/ )查看 moment.js 文档中的其他本地化格式 ( https://momentjs.com/docs/#/displaying/format/ )

I tried all of the solutions posted here but none of them worked for me.我尝试了这里发布的所有解决方案,但没有一个对我有用。 I managed to disable the time by using this line of code in my jQuery:我设法通过在我的 jQuery 中使用这行代码来禁用时间:

$('#datetimepicker4').datetimepicker({
    format: 'YYYY-MM-DD'
});

This set the format to date only and disabled the time completely.这将格式设置为仅日期并完全禁用时间。 Hope this helps.希望这可以帮助。

This is for: Eonasdan's Bootstrap Datetimepicker这适用于: Eonasdan 的 Bootstrap Datetimepicker

First of all I would provide an id attribute for the <input> and then initialize the datetimepicker directly for that <input> (and not for the parent container):首先,我会为<input>提供一个id属性,然后直接为该<input>初始化 datetimepicker(而不是为父容器):

<div class="container">
  <input data-format="yyyy-MM-dd" type="text" id="datetimepicker"/>
</div>
<script type="text/javascript">
  $(function() {
    // Bootstrap DateTimePicker v3
    $('#datetimepicker').datetimepicker({
      pickTime: false
    });
    // Bootstrap DateTimePicker v4
    $('#datetimepicker').datetimepicker({
      format: 'DD/MM/YYYY'
    });
  });
</script>

For v3: Contrary to Ck Maurya 's answer:对于 v3:与Ck Maurya的回答相反:

  • pickDate: false will disable the date and only allow to pick a time pickDate: false将禁用日期,只允许选择一个时间
  • pickTime: false will disable the time and only allow to pick a date (which is what you want). pickTime: false将禁用时间,只允许选择一个日期(这是你想要的)。

For v4: Bootstrap Datetimepicker now uses the format to determine if a time-component is present.对于 v4:Bootstrap Datetimepicker 现在使用该格式来确定是否存在时间组件。 If no time component is present, it won't let you choose a time (and hide the clock icon).如果不存在时间组件,则不会让您选择时间(并隐藏时钟图标)。

I spent some time trying to figure this out due to the update with DateTimePicker .由于DateTimePicker的更新,我花了一些时间试图弄清楚这一点。 You would enter in a format based off of moment.js documentation .您将以基于moment.js 文档的格式输入。 You can use what other answers showed:您可以使用其他答案显示的内容:

$('#datetimepicker').datetimepicker({ format: 'DD/MM/YYYY' });

Or you can use some of the localized formats moment.js provides to do just a date, such as:或者你可以使用 moment.js 提供的一些本地化格式来做一个日期,例如:

$('#datetimepicker').datetimepicker({locale: 'fr', format: 'L' });

Using this, you are able to display only a time ( LT ) or date ( L ) based on locale .使用它,您可以根据 locale 仅显示时间 ( LT ) 或日期 ( L ) The documentation for datetimepicker wasn't clear to me that it automatically adapts to the input provided (date or only time) via moment.js format.我不清楚 datetimepicker 的文档是否会自动适应通过moment.js格式提供的输入(日期或仅时间)。 Hope this helps for those still looking.希望这对那些仍在寻找的人有所帮助。

$('#datetimepicker').datetimepicker({ 
    minView: 2, 
    pickTime: false, 
    language: 'pt-BR' 
});

Please try if it works for you as well请尝试它是否也适合您

<div class="container">
  <input type="text" id="datetimepicker"/>
</div>

<script type="text/javascript">
  $(function() {
    // trigger datepicker
    $('#datetimepicker').datetimepicker({
       pickTime: false,
        minView: 2,
        format: 'dd/mm/yyyy',
        autoclose: true,
    });
  });
</script>

Initialize your datetimepicker like this像这样初始化你的日期时间选择器

For disable time对于禁用时间

$(document).ready(function(){
    $('#dp3').datetimepicker({
      pickTime: false
    });
});

For disable date对于禁用日期

$(document).ready(function(){
    $('#dp3').datetimepicker({
      pickDate: false
    });
});

To disable both date and time禁用日期和时间

$(document).ready(function(){
   $("#dp3").datetimepicker('disable'); 
});

please refer following documentation if you have any doubt如果您有任何疑问,请参阅以下文档

http://eonasdan.github.io/bootstrap-datetimepicker/#options http://eonasdan.github.io/bootstrap-datetimepicker/#options

This shows only date:这仅显示日期:

$('#myDateTimePicker').datetimepicker({
        format: 'dd.mm.yyyy',
        minView: 2,
        maxView: 4,
        autoclose: true
      });

More on the subject here更多关于这里的主题

For bootstrap 4 version this code working;对于 bootstrap 4 版本,此代码有效;

$('#payment-date-time-select').datetimepicker({
    startView:2,
    minView:2,
});
    $(function () {
        $('#datetimepicker9').datetimepicker({
            viewMode: 'years',
            format: 'DD/MM/YYYY' /*Add this line to remove time format*/
        });
    });
your same code work's fine, just add some link reference

<!DOCTYPE HTML>
<html>
    <head>
        <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" media="screen"
              href="http://tarruda.github.com/bootstrap-datetimepicker/assets/css/bootstrap-datetimepicker.min.css">


    </head>
    <body>
        <div class="well">
            <div id="datetimepicker4" class="input-append"> 
                <input data-format="yyyy-MM-dd" type="text"></input> 
                <span class="add-on"> 
                    <i data-time-icon="icon-time" data-date-icon="icon-calendar"> </i> 
                </span> 
            </div> 
        </div> 

        <script type="text/javascript"
                src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js">
        </script> 
        <script type="text/javascript"
                src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js">
        </script>
        <script type="text/javascript"
                src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.min.js">
        </script>
        <script type="text/javascript"
                src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.pt-BR.js">
        </script>
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker4').datetimepicker({pickTime: false});
            });
        </script>

    </body>
    </html>

The selector criteria is now an attribute the DIV tag.选择器标准现在是 DIV 标签的一个属性。

examples are ...例子是……

data-date-format="dd MM yyyy" 
data-date-format="dd MM yyyy - HH:ii p
data-date-format="hh:ii"

so the bootstrap example for dd mm yyyy is:-所以 dd mm yyyy 的引导程序示例是:-

<div class="input-group date form_date col-md-5" data-date="" 
    data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
..... etc ....
</div>

my Javascript settings are as follows:-我的 Javascript 设置如下:-

            var picker_settings = {
                language:  'en',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            };

        $(datePickerId).datetimepicker(picker_settings);

You can see working examples of these if you download the bootstrap-datetimepicker-master file.如果您下载 bootstrap-datetimepicker-master 文件,您可以看到这些工作示例。 There are sample folders each with an index.html.有示例文件夹,每个文件夹都有一个 index.html。

In my case, the option I used was:就我而言,我使用的选项是:

var from = $("input.datepicker").datetimepicker({
  format:'Y-m-d',
  timepicker:false # <- HERE
});

I'm using this plugin https://xdsoft.net/jqplugins/datetimepicker/我正在使用这个插件https://xdsoft.net/jqplugins/datetimepicker/

I know this is late, but the answer is simply removing "time" from the word, datetimepicker to change it to datepicker .我知道这已经晚了,但答案只是从单词datetimepicker删除“时间”以将其更改为datepicker You can format it with dateFormat .您可以使用dateFormat对其进行格式化。

      jQuery( "#datetimepicker4" ).datepicker({
        dateFormat: "MM dd, yy",
      });

Disable time in boostrap datetime picker...在 boostrap 日期时间选择器中禁用时间...

   $(document).ready(function () {
        $('.timepicker').datetimepicker({
            format: "dd-mm-yy",
        });
    });

Disable date in boostrap datetime picker...在 boostrap 日期时间选择器中禁用日期...

   $(document).ready(function () {
        $('.timepicker').datetimepicker({
            format: "HH:mm A",
        });
    });
<script type="text/javascript">
        $(function () {
                  $('#datepicker').datetimepicker({
                     format: 'YYYY/MM/DD'
               });
            });
</script>

This allows to show time in input field but hides time picker button, which is second li element in accordion这允许在输入字段中显示时间但隐藏时间选择器按钮,这是手风琴中的第二个 li 元素

.bootstrap-datetimepicker-widget .list-unstyled li:nth-child(2){
    display: none;
}

I am late to this post, but I want to share my experience.我迟到了这篇文章,但我想分享我的经验。 Some people suggest the code below to turn off the time picker and it did fix the issue.有些人建议使用下面的代码关闭时间选择器,它确实解决了问题。

$('#datetimepicker').datetimepicker({ 
    minView: 2, 
    pickTime: false
});

The reason behind I think they are using this library https://www.malot.fr/bootstrap-datetimepicker/ instead of this library http://eonasdan.github.io/bootstrap-datetimepicker/我认为他们使用这个库https://www.malot.fr/bootstrap-datetimepicker/而不是这个库http://eonasdan.github.io/bootstrap-datetimepicker/背后的原因

Here's the solution for you.这是为您提供的解决方案。 It's very easy to just add code like this:添加如下代码非常简单:

    $('#datetimepicker4').datetimepicker({ 

    pickTime: false 

    minview:2;(Please see here.)

    }); 

Not as put off time and language at a time I put this and not work不是一次推迟时间和语言,我把这个而不是工作

$(function () {
    $('#datetimepicker2').datetimepicker({
              locale: 'es',
              pickTime: false
          });
});
 $("#datetimepicker4").datepicker({
    dateFormat: 'mm/dd/yy',
    changeMonth: true,
    changeYear: true,
    showOtherMonths: true,
    selectOtherMonths: true,
    pickTime: false,
    format: 'YYYY-MM-DD'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM