2012-12-21 40 views
11

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

我得到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> 

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

+0

http://api.jqueryui.com/datepicker/#option-dateFormat –

回答

5

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

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

为什么这是公认的答案?问题是将格式设置为**用户的语言环境**,而不是** dd.mm.yy **。当OP说** 21.12.2012 **时,它只是德国格式的一个例子。我误解了一些东西吗? – Alisson

+0

我们可以将其设置为用户区域设置。因为我的网站在全球范围内运行,所有人都希望进入他们的格式。 –

11

使用dateFormat option更改为自定义格式

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

真正的问题是,你需要手动包括本地化文件。

他们可以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

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

+1

链接和演示已损坏 –

0

在网络上jQuery的文档可以看出,似乎玩捉迷藏&寻找日期选择器的国际化文件的位置,例如:

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

由于这张贴链接:

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

似乎是有效的。如果我将它包括在内,它本身并不起作用。我结束了 使用http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/i18n/jquery-ui-i18n.min.js这是不是我使用的1.12 jquery版本,但它似乎工作。

请注意我在这里使用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> 
0

因为似乎没有成为一个现成的区域设置信息,我决定使用有点不同的方法:我只用日期选择为选择日期,并从它检索日期本身,而是用于显示我用一个普通的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());