3

I have a requirement to change the order of DD/MM/YYYY tags according to a users country .

http://en.wikipedia.org/wiki/Date_format_by_country

The way that I was thinking to do this is to create a country / Dateformat table and according to the country selection to move the fields around using jquery .

Is there an existing way for this to be done in php or even in js or a better approach ? I was also looking for a table of country/ dateformat rather than inserting all the values manually but I couldn't find anything ...

scott.korin
  • 2,537
  • 2
  • 23
  • 36
Athanatos
  • 1,089
  • 6
  • 15
  • 32
  • Check out the term "locale". – Bart Friederichs Mar 10 '13 at 21:18
  • Take a look at http://jqueryui.com/datepicker/#localization and http://docs.jquery.com/UI/Datepicker/Localization – MatRt Mar 10 '13 at 21:21
  • Thanks for the answers everyone but I need a small example of code if possible to move the fields around using e.g the JS toLocaleString that was suggested , I have three select elements DD MM YYYY . The jquery example is good but it looks that you need to include a datepicker for each country. – Athanatos Mar 10 '13 at 21:36
  • By far the best solution is to use a single, unambiguous format (e.g. 10 March, 2013). If you attempt to "localise" the string, you will still get it wrong a good percentage of the time. Also, the implementation of `Date.prototype.toLocaleString` is implementation dependent, so you will still need to manually parse the date if you want to present it in a consistent format across all browsers (Some may do `Tuesday, 11 March 2013` and some may do `3/11/2013` and others `2013-03-11`). – RobG Mar 10 '13 at 23:19

3 Answers3

2

For PHP, this should be a good start: http://php.net/manual/en/function.setlocale.php

For JavaScript: Display date/time in user's locale format and time offset

All in all, most modern languages have locale support built-in very well. You should not have to implement this yourself. It will be tiresome and buggy (localization is hard).

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
2

if you use PHP then the IntlDateFormatter() helps you out:

$d = new DateTime();

$fmt = new IntlDateFormatter('en-US', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "US: ".$fmt->format($d)."<br/>";

$fmt = new IntlDateFormatter('en-GB', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "GB: ".$fmt->format($d)."<br/>";

$fmt = new IntlDateFormatter('en-AU', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "AU: ".$fmt->format($d)."<br/>";

$fmt = new IntlDateFormatter('de-DE', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
echo "DE: ".$fmt->format($d)."<br/>";

Output:

US: 2/1/18
GB: 01/02/2018
AU: 1/2/18
DE: 01.02.18
Zoltán Süle
  • 1,482
  • 19
  • 26
0

If you want to change the format of a date on the client side, you can try the toLocaleString function on the Date object in JavaScript. The toLocaleString will change the format based on the client OS's settings for their location. You also would not need to have a table with the country and date format.

This can be done without the need for jQuery or any additional plugin.

scott.korin
  • 2,537
  • 2
  • 23
  • 36
  • 1
    Good in theory but doesn't work in practice as not all browsers support it. Please don't reference W3Schools, [ECMA-262](http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.5) the definitive reference for language features, the [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString) is also a good resource. In this case, [toLocaleDateString](http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.6) is probably a better suggestion. – RobG Mar 10 '13 at 23:10