简体   繁体   English

bootstrap datepicker setDate 格式 dd/mm/yyyy

[英]bootstrap datepicker setDate format dd/mm/yyyy

I have to set the date in my datepicker in dd/mm/yyyy format.我必须在我的日期选择器中以 dd/mm/yyyy 格式设置日期。 Whet I'm trying to do, with Javascript is this:我正在尝试使用 Javascript 是这样的:

  var year = 2014;
  var month = 5;
  var day = 10;

  var realDate = new Date(year, month - 1, day); // months are 0-based!

  $('#MainContent_txtDataConsegna').datepicker({ dateFormat: 'dd/mm/yyyy' }); // format to show
  $('#MainContent_txtDataConsegna').datepicker('setDate', realDate);

the problem is that the value inside my datepicker is always in format mm/dd/yyyy.问题是我的日期选择器中的值始终采用 mm/dd/yyyy 格式。 Where is the problem??哪里有问题??

Here the definition datepicker and datepicker options:这里定义了 datepicker 和 datepicker 选项:

   <div class="input-group date">
       <asp:TextBox runat="server" ID="txtDataOrdine" class="form-control" onBlur="CalcolaTotaliTestata('txtDataOrdine')"></asp:TextBox>
       <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i>/span>
    </div>

<script type="text/javascript">
   $('.input-group.date').datepicker({
       todayBtn: "linked",
       language: "it",
       autoclose: true,
       todayHighlight: true,
       dateFormat: 'dd/mm/yyyy' 
   });
</script>

Change改变

dateFormat: 'dd/mm/yyyy'

to

format: 'dd/mm/yyyy'

Looks like you just misread some documentation!看起来您只是误读了一些文档!

更改为format: 'dd/mm/yyyy'对我不起作用,并将其更改为dateFormat: 'dd/mm/yyyy'多次添加年份,对我来说最好的是,

dateFormat: 'dd/mm/yy'

a bit change in format格式略有变化

format : "DD/MM/YYYY"格式:“DD/MM/YYYY”

There is confusing when you are using data picker, for bootstrap datepicker you have to use following code:-使用数据选择器时会令人困惑,对于引导日期选择器,您必须使用以下代码:-

$( ".datepicker" ).datepicker({
   format: 'yyyy-mm-dd'
});

and for jquery datepicker following code must be used:-对于 jquery datepicker,必须使用以下代码:-

$( ".datepicker" ).datepicker({
       dateFormat: 'dd-mm-yy'
    });

So you must be aware which date picker you are using, that is ultimately solve your issue, Hope this hepls you.所以你必须知道你正在使用哪个日期选择器,这最终可以解决你的问题,希望这对你有帮助。

for me it is daterangepicker worked by this :对我来说,它是由 daterangepicker 工作的:

     $('#host1field').daterangepicker({
                   locale: {
                            format: 'DD/MM/YYYY'
                            }
                 });

对我来说,我遇到了同样的问题,我解决了这样的更改格式:'dd/mm/yy' 到dateFormat :'dd/mm/yy'

You can use this code for bootstrap datepicker:您可以将此代码用于引导日期选择器:

HTML Code: HTML代码:

<p>Date: <input type="text" id="datepicker" class="datepicker"></p>

Javascript: Javascript:

$( ".datepicker" ).datepicker({
   format: 'yyyy-mm-dd'
});

format : "DD/MM/YYYY" should resolve your issue.格式:“DD/MM/YYYY”应该可以解决您的问题。 If you need more help regarding available formats, please visit http://momentjs.com/ which is used by this control internally.如果您需要有关可用格式的更多帮助,请访问此控件内部使用的http://momentjs.com/

I was facing an issue where this "format" thing was working fine on local machine but when I deployed the application on server, the date was not getting populated and the control was empty.我遇到了一个问题,这个“格式”的东西在本地机器上运行良好,但是当我在服务器上部署应用程序时,日期没有被填充并且控件是空的。 when I commented the format code, it started working but then I didn't have the format that I needed.当我评论格式代码时,它开始工作,但后来我没有我需要的格式。 I fixed that issue using globalization entries in web.config.我使用 web.config 中的全球化条目解决了该问题。

<system.web>
    <globalization
    requestEncoding="utf-8"
    responseEncoding="utf-8"
    culture="en-GB"
    uiCulture="en-GB" />    
</system.web>

This helped in ensuring that both local & server environments have same culture.这有助于确保本地和服务器环境具有相同的文化。

This line of code works for me这行代码对我有用

$("#datepicker").datepicker("option", "dateFormat", "dd/mm/yy");

you can change the id #datepicker with the class .input-group.date of course您当然可以使用类.input-group.date更改 id #datepicker

I have some problems with jquery mobile 1.4.5.我对 jquery mobile 1.4.5 有一些问题。 For example it seems accepting format change only passing from "option".例如,它似乎只接受从“选项”传递的格式更改。 And there are some refresh problem with the calendar using "option".使用“选项”的日历存在一些刷新问题。 For all that have the same problems I can suggest this code:对于所有有相同问题的人,我可以建议使用以下代码:

$( "#mydatepicker" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
$( "#mydatepicker" ).datepicker( "setDate", new Date());    
$('.ui-datepicker-calendar').hide();

"As with bootstrap's own plugins, datepicker provides a data-api that can be used to instantiate datepickers without the need for custom javascript..." “与引导程序自己的插件一样,datepicker 提供了一个数据 API,可用于实例化日期选择器,而无需自定义 javascript ......”

Boostrap DatePicker Documentation Boostrap 日期选择器文档

Maybe you want to set format without DatePicker instance , for that, you have to add the property data-date-format="DateFormat" to main Div tag, for example:也许您想设置没有 DatePicker 实例的格式,为此,您必须将属性data-date-format="DateFormat"到主 Div 标签,例如:

<div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd">
    <div class="input-group-addon">
        <span class="glyphicon glyphicon-th"></span>
    </div>

    <input type="text" name="YourName" id="YourId" class="YourClass">
</div>

Try this尝试这个

 format: 'DD/MM/YYYY hh:mm A'

As we all know JS is case-sensitive.众所周知,JS 是区分大小写的。 So this will display所以这将显示

10/05/2016 12:00 AM 10/05/2016 12:00 AM

In your case is在你的情况是

format: 'DD/MM/YYYY'

Display : 10/05/2016显示 : 10/05/2016

My bootstrap datetimepicker is based on eonasdan bootstrap-datetimepicker我的 bootstrap datetimepicker 基于eonasdan bootstrap-datetimepicker

As with bootstrap's own plugins, datepicker provides a data-api that can be used to instantiate datepickers without the need for custom javascript.. Use this for Bootstrap:与 bootstrap 自己的插件一样,datepicker 提供了一个数据 API,可用于实例化日期选择器,而无需自定义 javascript.. 将此用于 Bootstrap:

<label>Date of Birth</label>
<input name="userdob" data-provide="datepicker" placeholder="dd-mm-yyyy" date-autoclose="true" class="form-control courtdata" data-date-format="dd-mm-yyyy" required>

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

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