【问题标题】:How to detect the user's locale date and time format如何检测用户的区域设置日期和时间格式
【发布时间】:2011-07-29 18:13:21
【问题描述】:

是否有可能使用纯 Javascript 确定用户在其操作系统(Windows、Linux、MAC OS 等)上配置的日期时间FORMAT

提前致谢。

编辑:我知道 toLocaleString() 方法,但这无助于我获取客户端在其本地计算机上配置的格式

【问题讨论】:

  • 您是在问如何获取用户的日期时间格式,还是他们所在的时区?大多数问题听起来像前者,但你的结尾,“UTC”或“本地时间(基于我的系统设置)”听起来像你想要的区域。
  • 不,我不想要这个区域。我想要格式。感谢您的回复,我编辑了问题。

标签: javascript datetime


【解决方案1】:

这样的?

<script type="text/javascript">

    var d=new Date();
    document.write("Original form: ");
    document.write(d + "<br />");
    document.write("Formatted form: ");
    document.write(d.toLocaleString());

    //calculate change of the 2 dates
</script>

【讨论】:

  • 我不明白您如何计算这两个日期的变化并获得用户在其操作系统上配置的格式。
  • >
  • 我检查了这个问题,似乎 toLocaleStrings 是他正在寻找的。检查此函数的以下描述:The toLocaleString method relies on the underlying operating system in formatting datessource
【解决方案2】:

这是一个想法,可能行得通,也可能行不通。

创建一个所有元素都不同的日期,例如 1999 年 2 月 18 日 13:45,使用 toLocaleString(),然后根据元素的不同值识别元素。

可能有点复杂,我没有任何代码可以帮助解决它,但这是一个可以扔掉的想法,也许你可以利用它。


编辑:这里有一些代码:

var d = new Date(1999,1,18,13,45,0).toLocaleString();
document.write("<p>String: "+d+"</p>");
var f = d
    .replace(/1999/,"%Y")
    .replace(/99/,"%y")
    .replace(/F[^ ]{3,}/i,"%M")
    .replace(/F[^ ]+/i,"%m")
    .replace(/PM/,"%A")
    .replace(/pm/,"%a")
    .replace(/18[^ ]+/,"%d%S") // day number with suffix
    .replace(/18/,"%d")
    .replace(/13/,"%H")
    .replace(/1/,"%h")
    .replace(/45/,"%i")
    .replace(/00/,"%s");
    // optionally add something to detect the day of the week (Thursday, here)
document.write("<p>Format: "+f+"</p>");

输出:

String: 18 February 1999 13:45:00
Format: %d %M %Y %H:%i:%s

【讨论】:

    【解决方案3】:

    我用纯 javascript 编写了一些可以在 IE/Firefox/Chrome 中运行的东西。它会输出 MM/DD/YYYY 或 DD/MM/YYYY,... 取决于 toLocalDateString()。

    在 Safari 上不起作用,但 new Date().toLocalDateString() 也不起作用。

    这是jsFiddle

     //Create a known date string
    var y = new Date(2013, 9, 25);
    var lds = y.toLocaleDateString();
    
    //search for the position of the year, day, and month
    var yPosi = lds.search("2013");
    var dPosi = lds.search("25");
    var mPosi = lds.search("10");
    
    //Sometimes the month is displayed by the month name so guess where it is
    if(mPosi == -1)
    {
        mPosi = lds.search("9");
        if(mPosi == -1)
        {
            //if the year and day are not first then maybe month is first
            if(yPosi != 0 && dPosi != 0)
            {
                mPosi = 0;
            }
            //if year and day are not last then maybe month is last
            else if((yPosi+4 <  lds.length) && (dPosi+2 < lds.length)){
                mPosi = Infinity;
            }
            //otherwist is in the middle
            else  if(yPosi < dPosi){
                mPosi = ((dPosi - yPosi)/2) + yPosi;            
            }else if(dPosi < yPosi){
                mPosi = ((yPosi - dPosi)/2) + dPosi;
            }   
        }
    
    }
    
    
    var formatString="";
    var order = [yPosi, dPosi, mPosi];
    order.sort(function(a,b){return a-b});
    
    for(i=0; i < order.length; i++)
    {
        if(order[i] == yPosi)
        {
            formatString += "YYYY/";
        }else if(order[i] == dPosi){
            formatString += "DD/";
        }else if(order[i] == mPosi){
            formatString += "MM/";
        }
    }
    
    formatString = formatString.substring(0, formatString.length-1);
    
    $('#timeformat').html(formatString+" "+lds);
    

    【讨论】:

    • 我喜欢尼克的这个建议,但我还需要一种方法来尝试检测日期分隔符(在我的情况下是句点而不是正斜杠)所以我为它添加了一个脏检查:@ 987654322@
    猜你喜欢
    • 2020-11-12
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多