jQuery 如何从日期选择器设置语言环境格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13993619/
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:10:07  来源:igfitidea点击:

How to set locale format from datepicker?

jqueryjquery-uidatepickerjquery-ui-datepicker

提问by Mediator

How can I set the date format in jQuery UI's datepicker according to the user's locale?

如何根据用户的语言环境在 jQuery UI 的日期选择器中设置日期格式?

I get source html:

我得到源html:

<script type="text/javascript">
    $(function() {
        $(".datefield2").datepicker($.datepicker.regional['de']);
    });
</script>
<form action="/Home/TestDate" method="post">    <input class="datefield2 hasDatepicker valid" name="date" value="21.12.2012" type="text" id="date">
    <input type="submit" value="send">
</form>

If a select a date in the datepicker I get 12/21/2012, however I need 21.12.2012.

如果在我得到的日期选择器中选择一个日期12/21/2012,但是我需要21.12.2012.

采纳答案by Fiona - myaccessible.website

You can set the date format like this:

您可以像这样设置日期格式:

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

回答by Gabriele Petrioli

Use the dateFormatoptionto change to a custom format

使用dateFormat选项更改为自定义格式

$(function() {
    $(".datefield2").datepicker($.datepicker.regional['de']);
    $(".datefield2").datepicker('option','dateFormat','dd.mm.yy');
});


The real issue is that you need to manually include the localization files.

真正的问题是您需要手动包含本地化文件。

They can be found at https://github.com/jquery/jquery-ui/tree/master/ui/i18n
The one specific for german is https://github.com/jquery/jquery-ui/blob/master/ui/i18n/datepicker-de.js

它们可以在https://github.com/jquery/jquery-ui/tree/master/ui/i18n找到
。德语特定的一个是https://github.com/jquery/jquery-ui/blob/master/ ui/i18n/datepicker-de.js

Demo (with full localization) can be seen at http://jsfiddle.net/gaby/h07ny9ep/

演示(完全本地化)可以在http://jsfiddle.net/gaby/h07ny9ep/看到

回答by Wolfgang Fahl

The JQuery documentation on the web seems to play hide&seek for where the i18n files for the datepicker are see e.g.

网络上的 JQuery 文档似乎对日期选择器的 i18n 文件所在的位置进行了捉迷藏,例如

Where are the i18n files of jQuery UI datepicker >= 1.11.0

jQuery UI datepicker >= 1.11.0的i18n文件在哪里

As of this posting the link:

截至本次发布链接:

https://raw.githubusercontent.com/jquery/jquery-ui/master/ui/i18n/datepicker-de.js

https://raw.githubusercontent.com/jquery/jquery-ui/master/ui/i18n/datepicker-de.js

seems to be valid. If i included it it didn't work by itself. I ended up using http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/i18n/jquery-ui-i18n.min.jswhich is not the same version as the 1.12 jquery I am using but it seems to work.

似乎是有效的。如果我包含它,它本身就不起作用。我最终使用了http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/i18n/jquery-ui-i18n.min.js这与我使用的 1.12 jquery 版本不同,但它似乎工作。

Please note I am using iso date format 2017-03-18 as yy-mm-dd here instead of the german locale style.

请注意,我在这里使用 iso 日期格式 2017-03-18 作为 yy-mm-dd 而不是德国语言环境样式。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet"
    href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/i18n/jquery-ui-i18n.min.js"></script>
<script>
    $(function() {
        // https://stackoverflow.com/questions/494958/how-do-i-localize-the-jquery-ui-datepicker
        // https://stackoverflow.com/questions/13993619/how-to-set-locale-format-from-datepicker
        $(".datepicker").datepicker($.datepicker.regional['de']);
        $(".datepicker").datepicker('option','dateFormat','yy-mm-dd')
    });
</script>
</head>

回答by velis

Since there doesn't seem to be a readily available locale information, I decided to use a bit different approach: I only use datepicker for choosing the date and retrieving the date as such from it, but for display I use an ordinary div and Date.toLocaleDateString(). This way I needn't bother with retrieving the format or worse yet - providing them all myself.

由于似乎没有现成的语言环境信息,我决定使用一些不同的方法:我只使用 datepicker 来选择日期并从中检索日期,但为了显示,我使用普通的 div 和Date .toLocaleDateString()。这样我就不必费心检索格式或更糟了 - 自己提供所有这些。

JSFiddle

JSFiddle

HTML:

HTML:

<div style="position:relative">
  <div id="date-text">

  </div>
  <div style="position: absolute; left: 0; top: 0">
    <input type="text" size="10" id="dt" style="opacity:0"/>
  </div>
</div>

JS:

JS:

var today = new Date();
today.setHours(0,0,0,0);
$("#dt").datepicker({defaultDate: today}).on('change', function() {
  $("#date-text").html($("#dt").datepicker("getDate").toLocaleDateString());
});
$("#date-text").html(today.toLocaleDateString());