2

I am using a Dojo DateTextBox in my website which takes date format in form of contstraint like "dd-mm-yyyy" etc. I need to pick the date format string of the visitor's locale and pass it to this DateTextBox to display the date in local format. I do not need a way to get the formatted date but to get the format string.

  • why not just set the visitor's locale in data-dojo-config? Let Dojo pick the appropriate format for you. – peller Mar 09 '13 at 03:22

2 Answers2

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']);
});

See it in action: http://jsfiddle.net/phusick/4ZDCv/

Alternatively require directly the localization bundle via dojo/i18n plugin:

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/

Edit: dijit/form/DateTextBox handles locale itself, therefore it's likely all you need is setting formatLength:

<input
    data-dojo-type="dijit/form/DateTextBox"
    data-dojo-props="constraints: { formatLength: 'long' }"
/>

An example how it works with multiple locales on the page: http://jsfiddle.net/phusick/PhHwg/

phusick
  • 7,342
  • 1
  • 21
  • 26
0

Try this (reference: 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>
Community
  • 1
  • 1
Barun
  • 2,542
  • 33
  • 35