如何从datepicker设置语言环境格式?

时间:2022-11-30 08:45:32

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

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

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。

4 个解决方案

#1


4  

You can set the date format like this:

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

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

#2


13  

Use the dateFormat option to 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 http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/
The one specific for german is http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/jquery.ui.datepicker-de.js

它们可以在http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/找到。德语专用的是http://jquery-ui.googlecode.com/svn/tags/latest /ui/i18n/jquery.ui.datepicker-de.js

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

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

#3


0  

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

Web上的JQuery文档似乎可以隐藏和搜索datepicker的i18n文件所在的位置,例如

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

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

As of this posting the link:

截至发布链接:

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.js which 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://*.com/questions/494958/how-do-i-localize-the-jquery-ui-datepicker
        // https://*.com/questions/13993619/how-to-set-locale-format-from-datepicker
        $(".datepicker").datepicker($.datepicker.regional['de']);
        $(".datepicker").datepicker('option','dateFormat','yy-mm-dd')
    });
</script>
</head>

#4


0  

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

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:

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());

#1


4  

You can set the date format like this:

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

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

#2


13  

Use the dateFormat option to 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 http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/
The one specific for german is http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/jquery.ui.datepicker-de.js

它们可以在http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/找到。德语专用的是http://jquery-ui.googlecode.com/svn/tags/latest /ui/i18n/jquery.ui.datepicker-de.js

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

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

#3


0  

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

Web上的JQuery文档似乎可以隐藏和搜索datepicker的i18n文件所在的位置,例如

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

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

As of this posting the link:

截至发布链接:

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.js which 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://*.com/questions/494958/how-do-i-localize-the-jquery-ui-datepicker
        // https://*.com/questions/13993619/how-to-set-locale-format-from-datepicker
        $(".datepicker").datepicker($.datepicker.regional['de']);
        $(".datepicker").datepicker('option','dateFormat','yy-mm-dd')
    });
</script>
</head>

#4


0  

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

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:

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());