带有初始值的 JQuery UI Datepicker 日期格式问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2427504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:29:46  来源:igfitidea点击:

JQuery UI Datepicker date format issue with initial value

jquerydateformatdatepicker

提问by Mithun Sreedharan

I need to set the initial date for the date picker to 03/20/2010 in mm-dd-yyyyy format. I have done this

我需要以 mm-dd-yyyyy 格式将日期选择器的初始日期设置为 03/20/2010。我已经这样做了

<input id='datepicker' type='text' value='20/03/2010' />

But my problem is on clicking the field date picker populates with today's date as highlighed and no date selected, and up on selecting date value

但我的问题是点击字段日期选择器填充今天的日期作为高亮显示并且没有选择日期,并选择日期值

But when i change my input field like one below

但是当我改变我的输入字段时,如下所示

<input id='datepicker' type='text' value='03/20/2010' />

Date picker is populating March 20 as selected date and current date highlighted. But all in 'mm-dd-yyyy' format!

日期选择器填充 3 月 20 日作为选定日期和当前日期突出显示。但都是“mm-dd-yyyy”格式!

I want show all dates in 'dd-mm-yyyy' format. How can I solve this?

我想以“dd-mm-yyyy”格式显示所有日期。我该如何解决这个问题?

I tried some of the options and combinations below, but none of them solved my problem

我尝试了下面的一些选项和组合,但没有一个能解决我的问题

$("#datepicker").datepicker();
$("#datepicker").datepick({dateFormat: 'mm/dd/yyyy'});
$("#datepicker").formatDate('dd/mm/yy');
$('#datepicker').datepicker("setDate", new Date($("#datepicker").val()) );

Update: Unfortunately i cant set Date value march 20 directly via javascript sice it is dynamically changing and is stored in a PHP variable. I need to get the default value from input field itself.

更新:不幸的是,我无法直接通过 javascript sice 设置日期值 March 20,它会动态更改并存储在 PHP 变量中。我需要从输入字段本身获取默认值。

回答by Alex Bagnolini

$('#datepicker').datepicker('option', 'dateFormat', 'dd-mm-yy');

Year is specified as yfor 2 digits and yyfor 4 digits, as stated here.

年被指定为y2位和yy4个位数,如规定在这里

回答by Matt Ball

You need a combination of dateFormatand defaultDateoptions:

您需要组合dateFormatdefaultDate选项:

$("#datepicker").datepicker(
{
    dateFormat: 'mm/dd/yyyy',
    defaultDate: '20/03/2010'
});

there are, however, many different ways to specify the default date. I'm not sure what you need so you can reference the API here: http://docs.jquery.com/UI/Datepicker#option-defaultDate.

但是,有许多不同的方法可以指定默认日期。我不确定您需要什么,因此您可以在此处参考 API:http: //docs.jquery.com/UI/Datepicker#option-defaultDate

回答by Marco Francisco

mmequals minutes not months.
Use dd/MM/yyyyinstead.

mm等于分钟而不是月。
使用dd/MM/yyyy来代替。

回答by Bishal Paudel

Also remember that type="date"will make issue, so set typeas text.

还要记住这type="date"会产生问题,所以设置typetext.

回答by nabla

Not accurate!!!

不准确!!!

$("#datepicker").datepicker(
{
    dateFormat: 'mm/dd/yyyy',
    defaultDate: '20/03/2010'
});

You must to put the right format in dateFormat : for your example there is : dd/mm/yytwo mistake:

您必须在 dateFormat 中放置正确的格式:对于您的示例,有:dd/mm/yy两个错误:

  • day/month inversion
  • year is only two 'y' not four
  • 日月反转
  • 年份只有两个“y”而不是四个