我在我的网站中使用Dojo DateTextBox,它采用约束形式的日期格式,如"dd-mm-yyyy"
等。我需要选择访问者区域设置的日期格式字符串并将其传递给此DateTextBox以显示日期以本地格式。我不需要获取格式化日期的方法,但需要获取格式字符串。
答案 0 :(得分:1)
require(["dojo/i18n", "dojo/date/locale"], function(i18n) {
var defaultLocale = i18n.normalizeLocale();
var bundle = i18n.getLocalization("dojo.cldr", "gregorian", defaultLocale);
// all available formats
console.dir(bundle);
// some of them
console.log(bundle['dateFormat-full']);
console.log(bundle['dateFormat-long']);
console.log(bundle['dateFormat-medium']);
console.log(bundle['dateFormat-short']);
});
查看实际操作:http://jsfiddle.net/phusick/4ZDCv/
或者需要通过dojo/i18n
插件直接使用本地化软件包:
require(["dojo/i18n!dojo/cldr/nls/gregorian"], function(gregorian) {
console.dir(gregorian); // all available formats
console.log(gregorian['dateFormat-full']);
});
jsFiddle:http://jsfiddle.net/phusick/jJVEU/
编辑: dijit/form/DateTextBox
处理区域设置本身,因此您可能只需要设置formatLength
:
<input
data-dojo-type="dijit/form/DateTextBox"
data-dojo-props="constraints: { formatLength: 'long' }"
/>
它如何与页面上的多个区域设置一起使用的示例:http://jsfiddle.net/phusick/PhHwg/
答案 1 :(得分:0)
试试这个(参考:Where can I find documentation on formatting a date in JavaScript?):
<script type="text/javascript">
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
curr_date = (curr_date < 10 ? "0" + curr_date : curr_date);
curr_month = (curr_month < 10 ? "0" + curr_month : curr_month);
var formatted_date = "" + curr_date + "-" + curr_month + "-" + curr_year;
</script>