如果用户的机器使用12小时时钟(上午/下午)或24时钟(军事时间),则使用javascript检测 [英] Detect with javascript if user's machine is using 12 hour clock (am/pm) or 24 clock (military time)

查看:449
本文介绍了如果用户的机器使用12小时时钟(上午/下午)或24时钟(军事时间),则使用javascript检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测用户的机器是否使用12小时制(上午/下午)或24小时制(军事时间)?

Is it possible to detect if user's machine is using 12 hour clock (am/pm) or 24 hour clock (military time)?

一种方法是检查用户区域设置,但它只是大量的区域设置比较列表,而美国想要12小时时钟的人可以发送我的区域设置,而不是US_en,我无法知道她的偏好。与此同时,来自美国的某人可能会将她的机器设置为使用12小时时间格式而不需要12小时时钟。

One way would be to check users locales, but then it is just massive list of locale comparison and someone from U.S who wants 12 hour clock can send me just en locale, not US_en and I have no way of knowing her preferences. At the same time someone from U.S might be set her machine to use 12 hour time format and doesn't want 12 hour clock.

编辑:

date.toLocaleTimeString();

如同用户Mouser在下面建议的那样理论上是理论上的,但不幸的是它是 bugged (在Chrome和Windows上的新Opera上测试),由于某种原因,始终返回上午/下午时间。

Would work it theory, as user Mouser suggested below, but unfortunately it's bugged on WebKit browsers (tested on Chrome and new Opera on Windows) and for some reason always returns am/pm time.

示例: http://jsfiddle.net/sjuaL3p4/

所以我想我必须重新解释我的问题,如果有人知道如何在webkit浏览器上完成它。

So I guess I have to rephrase my question if anyone has an idea how to accomplish it on webkit browsers also.

推荐答案

此解决方案适用于大多数浏览器,但Chrome中存在错误。

This solution works in most browsers, but bugs in Chrome.

var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
var dateString = date.toLocaleTimeString();

//apparently toLocaleTimeString() has a bug in Chrome. toString() however returns 12/24 hour formats. If one of two contains AM/PM execute 12 hour coding.
if (dateString.match(/am|pm/i) || date.toString().match(/am|pm/i) )
{
    //12 hour clock
}
else
{
    //24 hour clock
}

解决方法Chrome;概念验证

对于Chrome来说,这是一个丑陋的解决方法。它通过反向地理位置嗅出用户 country_code 。对于使用12小时系统的国家/地区的阵列检查该代码。此解决方案很难看,因为您需要用户权限才能获取地理位置数据,如果用户区域设置不同,则会提供错误的信息,但会为您提供用户所在的国家/地区。 我强烈反对使用此示例。这纯粹是出于灵感的目的。我把它作为一个概念证明。

This is an ugly work-around for Chrome. It sniffs out the users country_code via reverse geolocation. That code is checked against an array with countries using the 12 hour system. This solution is ugly because you need user permission to get geolocation data and if the users locale is different it will give the wrong information, but will provide you with the country of the user. I strongly advise against using this example. It's purely for inspiration purposes. I made it up as a proof of concept.

function getClockChrome() {

    navigator.geolocation.getCurrentPosition(function(pos) {
        var url = "http://nominatim.openstreetmap.org/reverse?format=json&lat="+pos.coords.latitude+"&lon="+pos.coords.longitude+"&addressdetails=1&accept-language=en_US&json_callback=chromeClockCallBack";
        var script = document.createElement('script');
        script.src = url;
        document.body.appendChild(script);
    },

    function()
    {
        //no access to location
    },


    {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    }
    );

}

getClockChrome();

var dateCountryCode = ['US', 'GB', 'PH', 'CA', 'AU', 'NZ', 'IN', 'EG', 'SA', 'CO', 'PK', 'MY'];
function chromeClockCallBack(data)
{
    //Request succeeded
    if (dateCountryCode.indexOf(data.address.country_code.toUpperCase()) > -1)
    {
        alert("12 hour clock");
    }
    else
    {
        alert("24 hour clock");
    }
}

这篇关于如果用户的机器使用12小时时钟(上午/下午)或24时钟(军事时间),则使用javascript检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆