简体   繁体   English

设置datepicker的默认日期格式?

[英]Setting default date format for datepicker?

I have a page where I keep track of various dates. 我有一个页面,我跟踪各种日期。 So I end up using datepicker through jquery anywhere from 10-200 times. 所以我最终通过jquery在10到200次之间使用datepicker。 I want to set the default for all cases of datepicker to the 'yy-mm-dd' - but I can't seem to figure out how to set that? 我想将所有datepicker案例的默认值设置为'yy-mm-dd' - 但我似乎无法弄清楚如何设置它?

I end up using datepicker through jquery anywhere from 10-200 times. 我最终通过jquery使用datepicker从10到200次。

I'm assuming you're calling datepicker() separately for each input, probably by id . 我假设您为每个输入分别调用datepicker() ,可能是id There's no need for that. 没有必要这样做。

Use a class as a hook on your input: 使用class作为输入的钩子:

<input class="my-datepicker">

Then call datepicker on elements with that class with your default configuration: 然后使用默认配置在该类的元素上调用datepicker

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

When you need a different configuration, use/assign a different class name. 如果需要其他配置,请使用/指定其他类名。

You can also use setDefaults (probably what you're looking for): 您也可以使用setDefaults (可能是您正在寻找的):

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

Go to the file jquery.ui.datepicker.js . 转到文件jquery.ui.datepicker.js

function Datepicker()
{
     dateFormat: 'mm/dd/yy'
}

Change it to the format you prefer. 将其更改为您喜欢的格式。 It will change the format for your whole website. 它将改变整个网站的格式。

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

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