How to change Date format according to the country

后端 未结 3 1014
半阙折子戏
半阙折子戏 2021-01-21 22:29

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

相关标签:
3条回答
  • 2021-01-21 23:00

    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).

    0 讨论(0)
  • 2021-01-21 23:06

    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.

    0 讨论(0)
  • 2021-01-21 23:12

    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
    
    0 讨论(0)
提交回复
热议问题